玩转springboot之springboot异步执行

springboot异步执行

使用@EnableAsync开启异步执行

在接口方法上使用@Async注解进行标注,该接口是一个异步接口

自定义异步线程执行器

复制代码
@Configuration
public class CustomAsyncConfigurer implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(20);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("myThreadPool-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new AsyncUncaughtExceptionHandler() {
            @Override
            public void handleUncaughtException(Throwable ex, Method method, Object... params) {
                System.out.println("出现异常");
                ex.printStackTrace();
            }
        };
    }
}

https://zhhll.icu/2021/框架/springboot/基础/8.springboot异步执行/

本文由mdnice多平台发布

相关推荐
阿维的博客日记1 天前
Hippo4j 线程池监控平台部署手册
java·spring boot·后端
C+++Python1 天前
详细介绍一下Java泛型的通配符
java·windows·python
JosieBook1 天前
【数据库】时序预测能力的分级进化:TimechoAI如何让每一类用户都能精准预见未来
java·开发语言·数据库
一生了无挂1 天前
Java处理JSON技巧教学(从基础到高阶实战全覆盖)
java·开发语言·json
李白的天不白1 天前
使用 SmartAdmin 进行前后端开发
java·前端
swordbob1 天前
Spring 单例 Bean 是线程安全的吗?
java·开发语言
2601_951643771 天前
Python第一,Java跌出前三,C语言杀回来了
java·c语言·python·编程语言排行·技术趋势
IT 行者1 天前
GitHub Spec Kit 实战(五):/speckit.tasks 怎么拆——Spec Kit 五部曲收官
java·ai编程·claude
(Charon)1 天前
【C++ 面试高频基础:指针、引用、const、static、new/delete 总结】
java·开发语言
Yeats_Liao1 天前
Feed流系统设计(三):数据模型与存储设计,从表结构到Redis收件箱
java·javascript·redis