阿飞Javaer,转载请注明原创出处,谢谢!
EventBus来自于google-guava包中。源码注释如下:
Dispatches events to listeners, and provides ways for listeners to register themselves.
The EventBus allows publish-subscribe-style communication between components
without requiring the components to explicitly register with one another (and thus be
aware of each other). It is designed exclusively to replace traditional Java in-process
event distribution using explicit registration. It is not a general-purpose publish-subscribe
system, nor is it intended for interprocess communication.
翻译:将事件分派给监听器,并为监听器提供注册自己的方法。EventBus允许组件之间的发布 - 订阅式通信,而不需要组件彼此明确注册(并且因此彼此意识到)。 它专门用于使用显式注册替换传统的Java进程内事件分发。 它不是一个通用的发布 - 订阅系统,也不是用于进程间通信。
使用参考
关于EventBus的用例代码提取自sharding-jdbc源码,并结合lombok最大限度的简化:
- EventBusInstance–用于获取EventBus实例(饿汉式单例模式)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EventBusInstance {
private static final EventBus INSTANCE = new EventBus();
public static EventBus getInstance() {
return INS