java实现异步调用

java实现异步调用1、使用线程池的逻辑实现异步调用packagecom.ourlang.dataextract.controller;importcom.google.common.util.concurrent.ThreadFactoryBuilder;importcom.ourlang.dataextract.common.CommonResult;importcom.ourlang.dataextract.service.ISInPatientListService;importorg.apach

大家好,又见面了,我是你们的朋友全栈君。

1、使用线程池的逻辑实现异步调用

package com.ourlang.dataextract.controller;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.ourlang.dataextract.common.CommonResult;
import com.ourlang.dataextract.service.ISInPatientListService;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/** * 异步调用方法 * <p> * https://github.com/ourlang * </p> * * @author ourlang */
@RestController
public class AsynCallController { 
   

    private final ISInPatientListService inPatientListService;

    @Autowired
    public AsynCallController(ISInPatientListService inPatientListService) { 
   
        this.inPatientListService = inPatientListService;
    }

    /** * 创建线程池 实现异步调用方法 * @param serialNumber 住院号 * @param itemIds 需要的导入的项目ID集合用逗号(`,`)隔开 */
    private void createThead(String serialNumber, String itemIds) { 
   
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        //异步执行的方法
        singleThreadPool.execute(new PatientOtherData(serialNumber, itemIds));
        singleThreadPool.shutdown();

    }

    /** * */
    private class PatientOtherData implements Runnable { 
   
        private String serialNumber;
        private String itemIds;

        public PatientOtherData(String serialNumber, String itemIds) { 
   
            this.serialNumber = serialNumber;
            this.itemIds = itemIds;
        }

        @Override
        public void run() { 
   
            try { 
   
                //异步保存患者的其他数据
                savePatientOtherData(serialNumber, itemIds);
            } catch (Exception e) { 
   
                System.out.println(e);
            }

        }
    }


    /** * 保存患者的所有信息到我们的mysql数据库 * 保存患者主索引 (这张表暂时没有用) * isgPatientListService.savePrimaryIndexData(); * * @param itemIds 选择导入患者的哪些数据 * @param serialNumber 住院流水号 */
    @RequestMapping("/savePatientDataById")
    public CommonResult savePatientDataById(@RequestParam(name = "SERIAL_NUMBER") String serialNumber, @RequestParam(name = "itemIds") String itemIds) { 
   
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonResult.SUCCESS);
        commonResult.setMsg(CommonResult.SUCCESS_MESSAGE);
        // 1 2两点可以保证患者列表有数据
        // 1、保存患者就诊记录
        inPatientListService.saveInPatientList(serialNumber);
        System.out.println("serialNumber=" + serialNumber);
        System.out.println("itemIds=" + itemIds);
        //创建异步调用的线程池
        createThead(serialNumber, itemIds);

        return commonResult;
    }


    /** * 保存患者需要的其他数据 * * @param itemIds 需要的导入的项目ID集合用逗号(`,`)隔开 */
    public void savePatientOtherData(String serialNumber, String itemIds) throws Exception { 
   

        Thread.sleep(5000);
        System.out.println("savePatientOtherData--serialNumber=" + serialNumber);
        System.out.println("savePatientOtherData--itemIds=" + itemIds);
			//进行需要异步操作的一系列操作,比如存入数据库等等
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157323.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vue slot插槽_笔记本内存条插槽显示4个

    vue slot插槽_笔记本内存条插槽显示4个为什么使用slotslot(插槽)在生活中很多地方都有插槽,电脑usb的插槽,插板当中的电源插槽插槽的目的是为了让我们原来的设备具备更多的扩展性比如电脑的USB我们可以插入U盘,手机,鼠标,键

    2022年7月29日
    8
  • php cas单点登录

    php cas单点登录一、CAS简介1、结构体系从结构体系看,CAS包括两部分:CASServer和CASClient。1.1、CASServerCASServer负责完成对用户的认证工作,需要独立部署,CASServer会处理用户名/密码等凭证(Credentials)。1.2、CASClient负责处理对客户端受保护资源的访问请求,

    2022年6月4日
    127
  • IntelliJ IDEA创建maven web项目(IDEA新手适用)

    IntelliJ IDEA创建maven web项目(IDEA新手适用)PS:从eclipse刚转到IDEA,对于这个陌生的工具我表示无言,但听说很好用,也就试试,结果我几乎花了一晚上的时间才搭起来mavenweb项目,觉得在此给各位一个搭建mavenweb项目的教程,指出我踩过的各种坑!步骤一:首先先创建一个project,在这里就是创建一个maven的工作空间步骤二:按照下面的步骤操作就可以了,最后next首先,选择左边的maven然后在右…

    2022年6月26日
    55
  • Typora下载和简单使用教程

    Typora下载和简单使用教程Typora下载Typora官网下载地址根据自己的电脑选择不同的版本,我用的是Windows64位的系统的就选择DownloadBeta(×64)下载完Typora以后我们就可以使用markdown了。Typora的学习1.标题数量 标题级数 快捷键 一个#加空格加内容 一级标题 Ctrl+1 两个#加空格加内容 二级标题 Ctrl+2 三个#加空格加内容 三级标题 Ctrl+3 以此类..

    2022年5月19日
    46
  • 机器学习模型中的损失函数loss function

    机器学习模型中的损失函数loss functionimportmatplotlib.pyplotaspltimportnumpyasnpxmin,xmax=-4,4xx=np.linspace(xmin,xmax,100)plt.plot([xmin,0,0,xmax],[1,1,0,0],’k-‘,label=”Zero-oneloss”)plt.plot(xx,np.where(x

    2022年7月12日
    18
  • MySQL 如何实现递归查询?「建议收藏」

    MySQL 如何实现递归查询?「建议收藏」点击上方IT牧场,选择置顶或者星标技术干货每日送达!前言最近在做的业务场景涉及到了数据库的递归查询。我们公司用的Oracle,众所周知,Oracle自带有递归查询的功能,所以…

    2022年6月16日
    214

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号