Guava EventBus消息订阅发布

Flume配置文件可以动态生效,是因为使用了Guave EventBus特性。

EventBus使用方法代码如下:

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

public class MyEventBus {
    public static void main(String[] args) {
// 生命一个eventBus对象
EventBus eventBus = new EventBus("myEventBus");
// 声明一个需要被注册到eventBus的对象,即注册观察者,开始监听
MyEventBus myEventBus = new MyEventBus();
// 将myEventBus 注册到 eventBus上, 即订阅消息并执相应的动作,这里只是打印
eventBus.register(myEventBus);
// 发布消息,通知订阅者需要执行关于这个对象(String对象:hello world)的动作(打印String对象 hello world)
eventBus.post("Hello word!");
// 注销观察者,停止监听
eventBus.unregister(eventBus);
}


@Subscribe
public void onEvent(Object obj) {
if (obj != null) {
System.out.println("obj = " + obj);
}
}
}

转自:https://blog.csdn.net/alionsss/article/details/86773176

posted @ 2021-07-22 15:02  chengxingliu  阅读(153)  评论(0)    收藏  举报