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
Subscribeannotation; - 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 ...
随机推荐
- PHPExcel说明
下面是总结的几个使用方法include 'PHPExcel.php';include 'PHPExcel/Writer/Excel2007.php';//或者include 'PHPExcel/Wri ...
- (转)HTTP协议(2)
转自:http://kb.cnblogs.com/page/130970/ 作者 :小坦克 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无 ...
- 总结源码编译安装mysql
最近在学习源码编译安装LAMP.LNMP时,一直遇到一个难题,就是就是mysql无论怎么源码编译安装,到最后启动服务都提示"Starting MySQL.The server quit wi ...
- python实用函数
dir([obj]) 显示对象属性, 无参数显示全局变量的名字 help([obj]) 显示对象的文档字符串 int(obj) 将一个对象转换为整数 len(obj) 返回对象的长度 range([[ ...
- Android分类前言
柚子园项目搁置后,半年多时间里都在开发微信公众平台和在公司实习,用的都是python,django,bottle,已经很久没有开发android了.技术的东西,不用就容易生疏甚至忘掉.刚好现在需要写毕 ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- ModelState用法
ModelState.AddModelError:添加错误信息 ModelState是一个字典类型,这句话的作用是向ModelState中添加一条错误信息,第一个参数是Key,第二个参数是Value. ...
- MVC5 ModelBinder
MVC5 ModelBinder 什么是ModelBinding ASP.NET MVC中,所有的请求最终都会到达某个Controller中的某个Action并由该Action负责具体的处理和响应.为 ...
- IOS 获得通讯录中联系人的所有属性 备用参考
ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...
- selenium各种场景下的启动Firefox
开始学习selenium时为了启动Firefox可谓费尽周折,在大神的帮助下才堪堪搞定,走出了selenium的第一步:jdk1.8 + selenium_2.46 + Firefox国际版40.0. ...