Spring高级 事件监听器 (二)利用线程池异步发送事件
publishEvent 底层调用了一个SimpleApplicationEventMulticaster 来发布事件,属性有一个Executor 可以用来设置异步的方式
一、设置线程池
package com.mangoubiubiu.conf; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.SimpleApplicationEventMulticaster; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class Config { @Bean public ThreadPoolTaskExecutor executor(){ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(3);//核心线程数 executor.setMaxPoolSize(10);//最大线程数 executor.setQueueCapacity(100);//队列大小 return executor; } @Bean public SimpleApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor executor){ SimpleApplicationEventMulticaster multicaste = new SimpleApplicationEventMulticaster(); multicaste.setTaskExecutor(executor); return multicaste; } }
二 、发布事件后测试
有2个线程来发布事件
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16366241.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
Spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步