一、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 }