詳細介紹請看Spring DeferredResult 如何實現非同步化,從而大大增加長連線數量。
DeferredResult
provides an alternative to using a
Callable
for asynchronous request processing。 While a
Callable
is executed concurrently on
behalf of
(代表) the application, with a
DeferredResult
the application can produce the result from a thread of its choice。
DeferredResult
提供了一種使用
Callable
介面的方式來實現非同步請求 。 雖然
Callable
是代表應用程式併發執行的,但是使用
DeferredResultapplication
可以得到結果從它執行的執行緒中。
Prepare to using the DeferredResult application
// 我目前捕捉不到 他的onError()事件
@Slf4j@CrossOrigin@RestController@SpringBootApplicationpublic class DeferredresultSpringmvcApplication { public static void main(String[] args) { SpringApplication。run(DeferredresultSpringmvcApplication。class, args); } private static ExecutorService executors = Executors。newFixedThreadPool(10); @RequestMapping(“/get0”) public DeferredResult
我們看列印結果 發現
1。 get1 執行結果2019-10-12 12:44:17。549 INFO 2020 ——- [nio-8080-exec-2] c。s。DeferredresultSpringmvcApplication : controller 呼叫的 thread : http-nio-8080-exec-22019-10-12 12:44:17。549 INFO 2020 ——- [nio-8080-exec-2] c。s。DeferredresultSpringmvcApplication : controller 開始執行 2019-10-12 12:44:17。549 INFO 2020 ——- [nio-8080-exec-2] c。s。DeferredresultSpringmvcApplication : service 開始執行 2019-10-12 12:44:19。549 INFO 2020 ——- [nio-8080-exec-2] c。s。DeferredresultSpringmvcApplication : service 服務呼叫的執行緒 : http-nio-8080-exec-2 2019-10-12 12:44:19。549 INFO 2020 ——- [nio-8080-exec-2] c。s。DeferredresultSpringmvcApplication : service 執行完畢 2019-10-12 12:44:19。549 INFO 2020 ——- [nio-8080-exec-2] c。s。DeferredresultSpringmvcApplication : controller 執行完畢 2。 get2執行結果2019-10-12 12:44:47。993 INFO 2020 ——- [nio-8080-exec-4] c。s。DeferredresultSpringmvcApplication : controller 呼叫的 thread : http-nio-8080-exec-42019-10-12 12:44:47。993 INFO 2020 ——- [nio-8080-exec-4] c。s。DeferredresultSpringmvcApplication : controller 開始執行 2019-10-12 12:44:47。994 INFO 2020 ——- [nio-8080-exec-4] c。s。DeferredresultSpringmvcApplication : controller 執行完畢 2019-10-12 12:44:47。997 INFO 2020 ——- [pool-1-thread-1] c。s。DeferredresultSpringmvcApplication : service 開始執行 2019-10-12 12:44:49。999 INFO 2020 ——- [pool-1-thread-1] c。s。DeferredresultSpringmvcApplication : service 服務呼叫的執行緒 : pool-1-thread-1 2019-10-12 12:44:49。999 INFO 2020 ——- [pool-1-thread-1] c。s。DeferredresultSpringmvcApplication : service 執行完畢
那我們如何自己實現一個呢 ?
/** * 自定義簡單使用 * @return */ @RequestMapping(“/get3”) public MyCallable
讓我們看一下我們實現的 執行結果
2019-10-12 12:42:23。869 INFO 2020 ——- [nio-8080-exec-5] c。s。DeferredresultSpringmvcApplication : controller 呼叫的 thread : http-nio-8080-exec-52019-10-12 12:42:23。870 INFO 2020 ——- [nio-8080-exec-5] c。s。DeferredresultSpringmvcApplication : controller 開始執行 2019-10-12 12:42:23。872 INFO 2020 ——- [nio-8080-exec-5] c。s。DeferredresultSpringmvcApplication : controller 執行完畢 2019-10-12 12:42:23。875 INFO 2020 ——- [ task-1] c。s。DeferredresultSpringmvcApplication : service 開始執行 2019-10-12 12:42:25。875 INFO 2020 ——- [ task-1] c。s。DeferredresultSpringmvcApplication : service 服務呼叫的執行緒 : task-1 2019-10-12 12:42:25。875 INFO 2020 ——- [ task-1] c。s。DeferredresultSpringmvcApplication : service 執行完畢
這裡的task 執行緒是誰呢 ?
ThreadPoolTaskExecutor
是 springboot自帶的預設的 非同步執行緒池 , 我們可以自己設定
@Configurationpublic class AsyncPoolConfig { @Bean public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor(){ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor。setCorePoolSize(20); executor。setMaxPoolSize(200); executor。setQueueCapacity(25); executor。setKeepAliveSeconds(200); executor。setThreadNamePrefix(“asyncThread”); executor。setWaitForTasksToCompleteOnShutdown(true); executor。setAwaitTerminationSeconds(60); executor。setRejectedExecutionHandler(new ThreadPoolExecutor。CallerRunsPolicy()); executor。initialize(); return executor; }}
// 複雜的呢
/** * 自定義靈活性 增加程式碼的可拓展性,加入執行緒池 * @return */ @RequestMapping(“/get4”) public MyCallable2
執行結果
2019-10-12 12:42:44。879 INFO 2020 ——- [nio-8080-exec-8] c。s。DeferredresultSpringmvcApplication : controller 呼叫的 thread : http-nio-8080-exec-82019-10-12 12:42:44。880 INFO 2020 ——- [nio-8080-exec-8] c。s。DeferredresultSpringmvcApplication : controller 開始執行 2019-10-12 12:42:44。882 INFO 2020 ——- [nio-8080-exec-8] c。s。DeferredresultSpringmvcApplication : controller 執行完畢 2019-10-12 12:42:44。883 INFO 2020 ——- [pool-2-thread-1] c。s。DeferredresultSpringmvcApplication : service 開始執行 2019-10-12 12:42:46。884 INFO 2020 ——- [pool-2-thread-1] c。s。DeferredresultSpringmvcApplication : service 服務呼叫的執行緒 : pool-2-thread-1 2019-10-12 12:42:46。884 INFO 2020 ——- [pool-2-thread-1] c。s。DeferredresultSpringmvcApplication : service 執行完畢
感覺我寫的很詳細 , 其他的就不多說了 , 其實他的應用場景就是 當一個處理請求消耗很長的時間時 , 需要去使用這個 , 主要就是採用發揮一個非同步處理的執行緒事件, 不是返回一個結果 , 這點要明白