com.google.common.eventbus.EventBus介绍
以下内容直接翻译了EventBus的注释:
com.google.common.eventbus.EventBus介绍:
首先这个类是线程安全的,
分发事件到监听器,并提供相应的方式让监听器注册它们自己。
EventBus允许组件之间进行 “发布-订阅” 式的通信,而不需要这些组件彼此知道对方。EventBus是专门设计用来替代传统的Java进程内的使用显示注册方式的事件发布模式。EventBus不是一个通用的发布-订阅系统,也不是用于进程间通信。
接收事件
一个对象接收事件时,将这样做:
- 暴露一个public方法 ,称之为事件订阅者(subscriber),这个方法接收一个参数,参数的类型是事件期望的类型。
- 用@Subscribe注解标计这个方法
- 通过一个EventBus实例的register(Object)方法注册自己
提交事件
提交事件时,将简单的把事件对象作为参数去调用 EventBus实例的pose(Object)方法。EventBus实例将根据事件对象的类型决定如何路由这个事件对象给所有已注册的监听器。
事件的路由是基于事件对象的类型—— 一个事件将被交付给可以被分配的任意的订阅者。这个包括事件对象实现的接口,事件对象的所有的父类,所有被父类实现的接口。
当post方法被调用后,所有对这个事件进行注册的订阅者会按顺序进行消费, 所以订阅者会快速合理地运行。如果一个事件可能触发一个扩展的过程(比如数据库负载),生成一个线程或队列之后处理。(一种方便的方法,使用一个AsyncEventBus)。
订阅方法
事件订阅者的方法必需只能接受一个参数:事件对象。
订阅者方法如果抛出异常,EventBus实例将捕获和记录异常。很少有方案这样去处理错误,只是在开发时可用于帮助我们发现问题时会这样做。
EventBus实例保证在同时不会有多个线程调用,除非这个方法通过@AllowConcurrentEvents注解明确允许。如果这个注解没有出现,订阅者方法也无需担心方法被重入,除非在EventBus实例之外有代码调用该方法。
死事件
如果一个事件被提交了,但是没有相应的订阅者接受它,就可以认为这是一个死事件。然后会给系统一个机会来处理这个死事件。可以通过包装一个类DeadEvent的实例来处理这个死事件。然后可以写一个类专门负责订阅死事件。
如果一个订阅者监听的事件对象是所有事件的父类,比如这个事件订阅了一个Object的事件对象,那么将不会出现死事件。
原文如下:
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.
Receiving Events
To receive events, an object should:
- Expose a public method, known as the event subscriber, which accepts a single argument of the type of event desired;
- Mark it with a
Subscribe
annotation; - Pass itself to an EventBus instance's
register(Object)
method.
Posting Events
To post an event, simply provide the event object to the post(Object)
method. The EventBus instance will determine the type of event and route it to all registered listeners.
Events are routed based on their type — an event will be delivered to any subscriber for any type to which the event is assignable. This includes implemented interfaces, all superclasses, and all interfaces implemented by superclasses.
When post
is called, all registered subscribers for an event are run in sequence, so subscribers should be reasonably quick. If an event may trigger an extended process (such as a database load), spawn a thread or queue it for later. (For a convenient way to do this, use an AsyncEventBus
.)
Subscriber Methods
Event subscriber methods must accept only one argument: the event.
Subscribers should not, in general, throw. If they do, the EventBus will catch and log the exception. This is rarely the right solution for error handling and should not be relied upon; it is intended solely to help find problems during development.
The EventBus guarantees that it will not call a subscriber method from multiple threads simultaneously, unless the method explicitly allows it by bearing the AllowConcurrentEvents
annotation. If this annotation is not present, subscriber methods need not worry about being reentrant, unless also called from outside the EventBus.
Dead Events
If an event is posted, but no registered subscribers can accept it, it is considered "dead." To give the system a second chance to handle dead events, they are wrapped in an instance of DeadEvent
and reposted.
If a subscriber for a supertype of all events (such as Object) is registered, no event will ever be considered dead, and no DeadEvents will be generated. Accordingly, while DeadEvent extends Object
, a subscriber registered to receive any Object will never receive a DeadEvent.
com.google.common.eventbus.EventBus介绍的更多相关文章
- 出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误
使用selenium,出现java.lang.NoClassDefFoundError: com/google/common/base/Charsets异常错误 原因:selenium-server- ...
- java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Ljava/util/concurrent/ConcurrentMap;
在storm启动topo的时候,报错: java.lang.NoSuchMethodError: com.google.common.collect.Maps.newConcurrentMap()Lj ...
- java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from 解决
在用spark的yarn-cluster模式跑fpgrowth进行频繁项集挖掘的时候,报如下错误: ERROR yarn.ApplicationMaster: User class threw exc ...
- Selenium 运行时出现错误(java.lang.NoClassDefFoundError: com/google/common/base/Function)
已经写好了java脚本,点击运行的过程中如果出现如下的错误提示时: java.lang.NoClassDefFoundError: com/google/common/base/Function 问题 ...
- 异常:Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.base.Preconditions.che ckState(ZLjava/lang/String;I)V
Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.bas ...
- Caused by: java.lang.NoClassDefFoundError: com/google/common/base/MoreObjects
环境:jdk1.8 开发工具:IDEA 说明:今天在做springboot集成swagger2的时候,在启动程序的时候,报错 报错信息: Error starting ApplicationConte ...
- elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor
错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor() ...
- new AppiumDriver<>(new URL(url), capabilities) 报错 java.lang.NoSuchMethodError: com.google.common.base.Throwables.throwIfUnchecked(Ljava/lang/Throwable;)V
2017-10-11 17:37:02.102 INFO c.u.a.r.PrepareDriver:41 - appium server url : http://127.0.0.1:4723/wd ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: com/google/common/base/Function : Unsupported major.minor version 52.0的解决办法(图文详解)
不多说,直接上干货! 问题详情 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/goo ...
随机推荐
- JavaScript中的apply与call与arguments对象
(一) call方法 语法:presentObj.call(thisObj,arg1,arg2,arg3...) 参数thisObj :将被用作当前对象presentObj的对象. 当thisObj无 ...
- (LightOJ 1149) Factors and Multiples
题目链接:http://lightoj.com/volume_showproblem.php?problem=1149 Description You will be given two sets o ...
- 我忽略了的DOCTYPE!
最近不知道是不是因为天气的原因,瞌睡太多了,而且每天晚上都做梦,更奇怪的是每次做梦都能够连着上次没有做完的梦继续做.第二天上班又没有精神,人都快崩溃了!不说了,郁闷! 偶然看到一个问题:Doctype ...
- x86与x64与x86_64
x86是指intel的开发的一种32位指令集,从386开始时代开始的,一直沿用至今,是一种cisc指令集,所有intel早期的cpu,amd早期的cpu都支持这种指令集,ntel官方文档里面称为“IA ...
- 一个月时间整理《深入浅出Node.js》
今天终于把朴灵老师写的<深入浅出Node.js>给学习完了, 这本书不是一本简单的Node入门书籍,它没有停留在Node介绍或者框架.库的使用层面上,而是从不同的视角来揭示Node自己内在 ...
- python引入导入自定义模块和外部文件
参考:http://blog.csdn.net/devil_2009/article/details/15816237 项目中想使用以前的代码,或者什么样的需求致使你需要导入外部的包 如果是web 下 ...
- python3 中自带urllib库可下载图片到本地
刚从python3下载图片的语句python2的不太一样,具体python3语句如下: form urllib import request jpg_link = '......' #图片链接 re ...
- 要成为开发中最牛逼的测试,测试中最牛逼的开发。从今天起学python,写博客。--python基础学习(一)
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32Type & ...
- Pycharm使用技巧
1.代码配色,即主题 pycharm自带的配色方案都很难看,网上的配色方案又很难看,所以根据其他ide的Monokai配色方案,自己定义了一个. pycharm Monokai主题下载:http:// ...
- 让WPF的Popup不总置顶的解决方案
使用WPF的Popup的时候会发现有一个问题,它总是会置顶,只要Popup的StayOpen不设置为False,它就一直呆在最顶端,挡住其他的窗口. 解决方案是继承Popup重新定义控件PopupEx ...