玩转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多平台发布

相关推荐
wuqingshun3141592 分钟前
产生死锁的四个必要条件
java·jvm
青槿吖3 分钟前
第二篇:Spring MVC进阶:注解、返回值与参数接收的花式玩法
java·开发语言·后端·mysql·spring·mvc·mybatis
共享家95274 分钟前
Java入门(抽象类 与 接口)
java·开发语言
hanbr4 分钟前
C++ string类模拟实现(完整版,含全运算符重载)
java·开发语言
xUxIAOrUIII6 分钟前
【Go每日面试题】内存管理
java·开发语言·golang
森屿山茶7 分钟前
hot100题解 —— 146.LRU缓存
java·开发语言
gameboy0317 分钟前
SpringbootActuator未授权访问漏洞
java
⑩-7 分钟前
API 网关的作用?Spring Cloud Gateway 原理?
java·服务器·网络·spring cloud
大傻^7 分钟前
LangChain4j 记忆架构:ChatMemory、持久化与跨会话状态
java·人工智能·windows·架构·langchain4j
vx-bot55566610 分钟前
企业微信ipad协议的消息扩展字段与业务数据注入
java·企业微信·ipad