SpringBoot 配置 异步 AsyncEventBus

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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。