博客
关于我
springboot异步线程管理AsyncConfigurer
阅读量:796 次
发布时间:2023-03-22

本文共 1719 字,大约阅读时间需要 5 分钟。

package com.common.base.config;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.util.concurrent Executor;
/**
* @Author: tony_t_peng
* @Date: 2020-08-10 10:06
*
* 支持使用@Async注解进行异步线程处理
* @Async方法将在独立线程池中执行
* 该线程池由本配置类管理,无法被@ControllerAdvice全局异常处理器捕获
* 异常处理需要手动实现
*/
@Configuration
@EnableAsync
public class BaseAsyncConfigurer implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 5);
executor.setQueueCapacity(Runtime.getRuntime().availableProcessors() * 2);
executor.setThreadNamePrefix("this-excutor-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (Throwable ex, Method method, Object... params) -> {
System.out.println("class#method: " + method.getDeclaringClass().getName() + "#" + method.getName());
System.out.println("type : " + ex.getClass().getName());
System.out.println("exception : " + ex.getMessage());
};
}
}

支持Spring Boot中使用@Async注解进行异步线程处理
@Async注解会启用独立线程池执行方法,无法被@RestControllerAdvice捕获
异步方法的异常处理需要手动实现

转载地址:http://cjqfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现FigurateNumber垛积数算法(附完整源码)
查看>>
Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
查看>>
Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
查看>>
Objective-C实现hamming numbers汉明数算法(附完整源码)
查看>>
Objective-C实现hanning 窗(附完整源码)
查看>>
Objective-C实现hanoiTower汉诺塔算法(附完整源码)
查看>>
Objective-C实现hardy ramanujana定理算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现hornerMethod霍纳法算法(附完整源码)
查看>>
Objective-C实现Http Post请求(附完整源码)
查看>>
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>
Objective-C实现IIR数字滤波器(附完整源码)
查看>>
Objective-C实现insertion sort插入排序算法(附完整源码)
查看>>
Objective-C实现integer partition整数分区算法(附完整源码)
查看>>
Objective-C实现integerPartition整数划分算法(附完整源码)
查看>>
Objective-C实现interpolation search插值搜索算法(附完整源码)
查看>>
Objective-C实现Interpolation search插值查找算法(附完整源码)
查看>>