Guava之EventBus

2020-09-03  本文已影响0人  神豪VS勇士赢

EventBus 采用推的方式将消息发送给订阅者

public class SimpleListener
{
    private final static Logger LOGGER = LoggerFactory.getLogger(SimpleListener.class);

    @Subscribe
    public void doAction(final String event)
    {
        if (LOGGER.isInfoEnabled())
        {
            LOGGER.info("Received event [{}] and will take a action", event);
        }
    }
}

public class SimpleEventBusExample
{
    public static void main(String[] args)
    {
        final EventBus eventBus = new EventBus();
        eventBus.register(new SimpleListener());
        System.out.println("post the simple event.");
        eventBus.post("Simple Event");
    }
}

1)
Listener存在继承关系 AListener extend BListener 并且接收的消息类型都是 同一类型
生产者生产同一类型的事件 AListener 和 BListener 都能收到事件消息 ,并进行处理 。

2)
发送事件 有继承关系 AEvent extend BEvent
Listener 里面两个subScribe 一个监听 AEvent 一个监听BEvent
当生产者发送 AEvent 两个 方法都能接收到消息
当生产者发送BEvent 只有 监听 BEvent才能收到消息

http://hawkeye.longhu.net/d/frCiDkznk/nei-rong-pei-zhi-zhong-xin-cms?orgId=1&refresh=10s&from=1625472008110&to=1625472308110&var-Datasource=prod&var-env=prod%2B30385687c6c5a46c428d4bcca48c99a584cf6862

上一篇 下一篇

猜你喜欢

热点阅读