Spring的线程池ThreadPoolTaskExecutor使用案例
2023-09-27 14:25:06 时间
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、定义任务类
1 package com.syj.phis.tools.test; 2 3 import java.io.Serializable; 4 import java.util.concurrent.Callable; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.annotation.Scope; 7 import org.springframework.stereotype.Component; 8 9 import com.syj.phis.ehr.entity.EhrBase; 10 import com.syj.phis.ehr.service.EhrBaseService; 11 /** 12 * 获取个人档案的任务类: 13 * 14 * 1.将任务类所需要的参数,声明为全局变量,并提供set方法. 15 * 2.实现Callable接口(Callable接口支持返回值,而Runnable接口不支持),并重写其call方法,将要业务代码写在call方法中. 16 * 17 * @author shiyanjun 18 */ 19 @Component 20 @Scope("prototype") 21 public class EhrDownloadTask implements Callable<EhrBase>, Serializable { 22 23 private static final long serialVersionUID = -6626027616177700489L; 24 25 @Autowired 26 private EhrBaseService ehrBaseService; 27 private String ehrId; 28 29 public void setEhrId(String ehrId) { 30 this.ehrId = ehrId; 31 } 32 33 /** 34 * 根据ehrId获取档案 35 */ 36 public EhrBase call() throws Exception { 37 38 //打印当前线程的名称 39 System.out.println(Thread.currentThread().getName() + "...."); 40 return (EhrBase) ehrBaseService.findByEhrId(ehrId); 41 } 42 }
3、测试
1 package com.syj.phis.tools.test; 2 3 import java.util.concurrent.Future; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.http.MediaType; 7 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.web.bind.annotation.PathVariable; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.ResponseBody; 13 14 import com.syj.phis.ehr.entity.EhrBase; 15 /** 16 * 多线程任务测试类: 17 * 18 * 1.使用Spring提供的线程池ThreadPoolTaskExecutor执行线程任务. 19 * 2.通过set方法传递参数. 20 * 3.使用Future对象封装返回值. 21 * 4.将每一个任务类使用@Autowired注解,交给Spring管理. 22 * 23 * @author shiyanjun 24 */ 25 @Controller 26 @RequestMapping(value = "/thread/pool/test") 27 public class ThreadPoolController { 28 @Autowired 29 private ThreadPoolTaskExecutor poolTaskExecutor; 30 @Autowired 31 private EhrDownloadTask ehrDownloadTask; 32 33 /** 34 * 根据ehrId获取档案 35 * 请求路径示例:http://localhost:8080/phis/app/thread/pool/test/ehr/5065a1f1-c990-47f5-a58b-dd8fb240c215 36 * @param ehrId 37 * @return 38 */ 39 @RequestMapping(value = "ehr/{ehrId}", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE) 40 @ResponseBody 41 public EhrBase download(@PathVariable("ehrId") String ehrId){ 42 43 ehrDownloadTask.setEhrId(ehrId); 44 45 //将任务交给Spring的线程任务执行器处理 46 Future<EhrBase> future = poolTaskExecutor.submit(ehrDownloadTask); 47 try { 48 //获取返回值 49 return future.get(); 50 } catch (Exception e) { 51 throw new RuntimeException(e); 52 } 53 } 54 }
相关文章
- 关于spring中使用mysql数据库明明配置了事务管理却失效的原因及解决方法
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- 《Spring Boot官方文档》18. 使用 @SpringBootApplication注解
- Too many open files ( spring-cloud-gateway )
- 专家有奖问答|Spring Boot我该怎么学?
- spring boot实战(第一篇)第一个案例
- Spring Cloud Gateway实战案例(限流、熔断回退、跨域、统一异常处理和重试机制)
- Spring boot 2.0 版本报错 ResourceHttpRequestHandler cannot be cast to HandlerMethod
- 面试题: Spring框架的好处
- Spring事务处理时自我调用的解决方案 嵌套AOP
- 《Servlet、JSP和Spring MVC初学指南》——第2章 会话管理 2.1URL重写
- Spring的IOC逐层深入——依赖注入的两种实现类型
- Spring Security OAuth正式终止维护,已从官网下架
- spring boot容器启动详解
- Java Spring-DI入门案例
- redis在linux下安装并測试(在spring下调用)
- Spring RESTFul Client – RestTemplate Example--转载
- Spring学习笔记(一)---IOC入门案例