【Java】Guava的EventBus实现PubSub(进程间)

本文通过一个删除项目的示例,详细介绍了如何利用Guava的EventBus实现进程间的PubSub(发布/订阅)模式。文章分为五个部分,包括事件发布/订阅中心的异步操作,任务删除事件的定义,事件监听者的实现,事件的发布以及实际执行的效果展示。
摘要由CSDN通过智能技术生成

以删除一个项目,将级联删除该项目下所有任务为例。

一、事件发布/订阅中心

可以看到事件的发布和订阅都采用的是异步的方式。

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 进程内的事件发布/订阅中心
 */
public class InternalEventCenter {

    private final static EventBus eventBus;

    static {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(5);
        threadPoolTaskExecutor.setMaxPoolSize(5);
        threadPoolTaskExecutor.setQueueCapacity(10);
        threadPoolTaskExecutor.setThreadNamePrefix("event-bus-task");
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.initialize();

        eventBus = new AsyncEventBus(threadPoolTaskEx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值