欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现异步调用

程序员文章站 2022-07-10 21:02:45
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.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://blog.csdn.net/qq_37493556/article/details/107493653