一、Sping配置文件java
<!-- 线程池配置 --> <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>
二、定义任务类web
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 }
三、测试spring
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 }