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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

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

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