日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot异步任务线程池

發布時間:2024/9/27 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot异步任务线程池 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

            • 1. 啟動類添加@EnableAsync注解
            • 2. 異步方法添加@Async注解
            • 3. 自定義線程池以及線程池異常策略

1. 啟動類添加@EnableAsync注解
package com.gblfy;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync;@EnableAsync @SpringBootApplication public class JavaEscapeApplication {public static void main(String[] args) {SpringApplication.run(JavaEscapeApplication.class, args);}}
2. 異步方法添加@Async注解
package com.gblfy.async_task;import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service;import java.util.concurrent.Future;@Slf4j @Service public class AsyncService {@Asyncpublic void asyncProcess01() throws Exception {log.info("AsyncService Start to asyncProcess01 ->{}", Thread.currentThread().getName());Thread.sleep(2000);log.info("AsyncService Start to asyncProcess01 ->{}", Thread.currentThread().getName());}@Asyncpublic Future<String> asyncProcess02() throws Exception {log.info("AsyncService Start to asyncProcess02->{}", Thread.currentThread().getName());Thread.sleep(2000);log.info("AsyncService Done to asyncProcess02->{}", Thread.currentThread().getName());return new AsyncResult<>("imooc");}@Asyncpublic void asyncProcess03() {log.info("AsyncService Start to asyncProcess03->{}", Thread.currentThread().getName());throw new RuntimeException("throw exception asyncProcess03");} }
3. 自定義線程池以及線程池異常策略
package com.gblfy.async_task;import lombok.extern.slf4j.Slf4j; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.lang.reflect.Method; import java.util.Arrays; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor;/*** 自定義異步任務線程池*/ @Slf4j @Configuration public class AsyncTaskConfig implements AsyncConfigurer {@Overridepublic Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setThreadNamePrefix("cmiip-");executor.setCorePoolSize(15);executor.setMaxPoolSize(20);executor.setKeepAliveSeconds(5);executor.setQueueCapacity(100);executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());executor.setWaitForTasksToCompleteOnShutdown(true);executor.setAwaitTerminationSeconds(60);executor.initialize();return executor;}@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {return new AsyncUncaughtExceptionHandler() {@Overridepublic void handleUncaughtException(Throwable ex, Method method, Object... params) {// 報警郵件,發短信等等log.error("async task some Error: {} ,{} , {}", ex.getMessage(),method.getDeclaringClass().getName() + "." + method.getName(),Arrays.toString(params));}};} }

測試

package com.gblfy.async_task;import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.concurrent.Future; import java.util.concurrent.TimeUnit;@Slf4j @SpringBootTest class AsyncServiceTest {@Autowiredprivate AsyncService asyncService;@Testvoid contextLoads() throws Exception { // asyncService.asyncProcess01();Future<String> future= asyncService.asyncProcess02();log.info("Async Process02 return :->{}", future.get(1, TimeUnit.SECONDS));}@Testvoid contextLoads2() throws Exception {asyncService.asyncProcess03();Thread.sleep(3000);} }

總結

以上是生活随笔為你收集整理的Springboot异步任务线程池的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。