1-在使用之前,先说明:
guava 的 事件操作分类型: 1-同步 EventBus 2-异步:AsyncEventBus
咱们这里说明异步的使用:
2-配置 线程自己控制
@Bean
public AsyncEventBusasyncEventBus() {
// 创建一个核心3线程,最大10线程的线程池,配置DiscardPolicy策略,抛弃当前的任务
ThreadPoolExecutor threadPoolExecutor =new ThreadPoolExecutor(3, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadPoolExecutor.DiscardPolicy());
return new AsyncEventBus(threadPoolExecutor);
}
3 -注册使用:例如:日志的异步记录
@Component
public class AsyncEventListener {
private AsyncEventBusasyncEventBus;
AsyncEventListener(AsyncEventBus asyncEventBus,SysLoginLogService sysLoginLogService){
this.asyncEventBus = asyncEventBus;
this.sysLoginLogService = sysLoginLogService;
}
/**
* 注册这个监听器
*/
@PostConstruct
public void register() {
asyncEventBus.register(this);
}
/**
* 添加登录日志信息
* @param sysLoginLog 登录日志
*/
@Subscribe
public void addLoginLog(SysLoginLog sysLoginLog) {
sysLoginLogService.save(sysLoginLog);
}
}
4- 各个接口的调用 使用异步的post
AsyncEventBus asyncEventBus;
asyncEventBus.post(sysLoginLog)