当前位置:网站首页>@Async注解的作用以及如何实现异步监听机制
@Async注解的作用以及如何实现异步监听机制
2022-08-04 23:08:00 【步尔斯特】
使用@Async标注在方法上,可以使该方法异步的调用执行。而所有异步方法的实际执行是交给TaskExecutor的。
在需要异步执行的类上标注@Async,比如,我们现在有一个需要监听的事件
public class HelloEven extends ApplicationEvent {
private String name;
public HelloEven(Object source,String name) {
super(source);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
异步监听器实现
@Component
public class HelloListen {
@EventListener
// 定义的线程池名称
@Async("taskExecutor")
public void run(HelloEven even) {
System.out.println("==== " + Thread.currentThread().getName() + " ====" + even.getName());
}
}
// 输出结果
// ==== Async-Service-1 ====hello,baby
定义线程池
/** * 默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务, * 当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; * 当队列满了,就继续创建线程,当线程数量大于等于maxPoolSize后,开始使用拒绝策略拒绝 */
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
/** * 核心线程数(默认线程数) */
private static final int corePoolSize = 20;
/** * 最大线程数 */
private static final int maxPoolSize = 100;
/** * 允许线程空闲时间(单位:默认为秒) */
private static final int keepAliveTime = 10;
/** * 缓冲队列大小 */
private static final int queueCapacity = 200;
/** * 线程池名前缀 */
private static final String threadNamePrefix = "Async-Service-";
@Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}
如下方式会使@Async失效
- 异步方法使用static修饰
- 异步类没有使用@Component注解(或其他注解)导致spring无法扫描到异步类
- 异步方法不能与被调用的异步方法在同一个类中
- 类中需要使用@Autowired或@Resource等注解自动注入,不能自己手动new对象
- 如果使用SpringBoot框架必须在启动类中增加@EnableAsync注解
边栏推荐
猜你喜欢

I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
![[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)](/img/b6/5a1c8b675dc7f67f359c25908403e1.png)
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)

365天深度学习训练营-学习线路

xss总结

【转载】kill掉垃圾进程(在资源管理器占用的情况下)

3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊

App测试和Web测试的区别

【3D建模制作技巧分享】ZBrush如何重新拓扑

OPENCV学习DAY8

被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
随机推荐
ffplay视频播放原理分析
一点点读懂Thremal(二)
If you can't get your heart, use "distributed lock" to lock your people
【云原生 · Kubernetes】Kubernetes运维
PID控制器改进笔记之七:改进PID控制器之防超调设定
The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
直播带货为农产品开拓销售渠道
BUG | The interface returns abnormal data
历史上的今天:PHP公开发布;iPhone 4 问世;万维网之父诞生
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
kernel hung_task死锁检测机制原理实现
测试技术:关于上下文驱动测试的总结
Using ngrok to optimize web pages on raspberry pi (2)
MySQL增删改查基础
年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
The Record of Reminding myself
Linear DP (bottom)
智慧养老整体解决方案
Service Mesh landing path
postman接口测试