java 异步调用实现_java实现异步调用

本文介绍如何使用Spring Boot创建线程池实现异步调用,通过`ISInPatientListService`服务,展示如何在`AsynCallController`中创建ThreadPoolExecutor,并演示了`PatientOtherData`线程任务的执行。重点在于`savePatientDataById`方法中,通过`@RequestParam`接收参数并启动异步处理。

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;

/**

* 异步调用方法

*

* https://github.com/ourlang

*

*

* @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);

//进行需要异步操作的一系列操作,比如存入数据库等等

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值