Spring的线程池ThreadPoolTaskExecutor使用案例

时间:2023-03-09 00:08:43
Spring的线程池ThreadPoolTaskExecutor使用案例

1、Sping配置文件

<!-- 线程池配置 -->
<bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="10" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="50" />
<!-- 队列最大长度 -->
<property name="queueCapacity" value="1000" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="300" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>

2、定义任务类

 package com.syj.phis.tools.test;

 import java.io.Serializable;
import java.util.concurrent.Callable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.syj.phis.ehr.entity.EhrBase;
import com.syj.phis.ehr.service.EhrBaseService;
/**
* 获取个人档案的任务类:
*
* 1.将任务类所需要的参数,声明为全局变量,并提供set方法.
* 2.实现Callable接口(Callable接口支持返回值,而Runnable接口不支持),并重写其call方法,将要业务代码写在call方法中.
*
* @author shiyanjun
*/
@Component
@Scope("prototype")
public class EhrDownloadTask implements Callable<EhrBase>, Serializable { private static final long serialVersionUID = -6626027616177700489L; @Autowired
private EhrBaseService ehrBaseService;
private String ehrId; public void setEhrId(String ehrId) {
this.ehrId = ehrId;
} /**
* 根据ehrId获取档案
*/
public EhrBase call() throws Exception { //打印当前线程的名称
System.out.println(Thread.currentThread().getName() + "....");
return (EhrBase) ehrBaseService.findByEhrId(ehrId);
}
}

3、测试

 package com.syj.phis.tools.test;

 import java.util.concurrent.Future;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.syj.phis.ehr.entity.EhrBase;
/**
* 多线程任务测试类:
*
* 1.使用Spring提供的线程池ThreadPoolTaskExecutor执行线程任务.
* 2.通过set方法传递参数.
* 3.使用Future对象封装返回值.
* 4.将每一个任务类使用@Autowired注解,交给Spring管理.
*
* @author shiyanjun
*/
@Controller
@RequestMapping(value = "/thread/pool/test")
public class ThreadPoolController {
@Autowired
private ThreadPoolTaskExecutor poolTaskExecutor;
@Autowired
private EhrDownloadTask ehrDownloadTask; /**
* 根据ehrId获取档案
* 请求路径示例:http://localhost:8080/phis/app/thread/pool/test/ehr/5065a1f1-c990-47f5-a58b-dd8fb240c215
* @param ehrId
* @return
*/
@RequestMapping(value = "ehr/{ehrId}", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public EhrBase download(@PathVariable("ehrId") String ehrId){ ehrDownloadTask.setEhrId(ehrId); //将任务交给Spring的线程任务执行器处理
Future<EhrBase> future = poolTaskExecutor.submit(ehrDownloadTask);
try {
//获取返回值
return future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}