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 ...
随机推荐
- OC4_可变数组
// // main.m // OC4_可变数组 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuemi ...
- 初识IO流之小型资源管理器
初次接触到IO流,根据书本上的知识,加上自己的摸索,发现了一些好玩的事情.(书本上的知识或多或少,有时候不足以解决我们的问题!这时候我们就应该自己去求解!!! 所以我们学习的时候要抱有探索的精神,求知 ...
- mysql---左连接、右连接、内连接之间的区别与联系
现有两张表 第一张表为男生表,记录了男生的姓名和配偶的编号 第二张表为女生表,记录了女生的姓名和自己的编号 第一种情况:主持人请所有男生都上台,并且带上自己的配偶.这时不管男生有没有配偶都要上台,所以 ...
- ViewState压缩
/// <summary> ///CompressViewState 的摘要说明 /// </summary> public class CompressViewState:S ...
- 手动将MySQL服务安装到windows中
我的系统是win7 x64,mysql版本5.6 (网上的老教程很多都没用了,版本太旧,这是我自己总结出来的方法) 下载zip包的mysql可以获得最新版本,还可以免安装,好处多多,但是要把mysql ...
- [CSS]font- 属性
所有浏览器都支持 font 属性. 注释:任何的版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit". 定义和用法 font 简写属性在一 ...
- Mysql zip压缩包安装
解压mysql.zip 配置环境变量(略) 配置my-default.ini 配置文件 安装mysql:mysqld -install 初始化mysql:mysqld --initialize 启动服 ...
- 2014年度辛星css教程夏季版第三节
第二节我们讲述的几乎全是CSS的选择器,那么下面这一节我们来讲一下CSS的颜色和文本的一些东西,虽然我对调色不大敏感,但是对于颜色还是比较感兴趣的. *********CSS中的颜色********* ...
- delphi xe5 android listbox的 TMetropolisUIListBoxItem
listbox实现以下效果: \ 关键代码,采用数据集的方式 type PpatientData=^RpatientData; RpatientData= record patient_id:stri ...
- hdu 4778
知道是状态压缩,但是不会做: 看题解学的: dp[i]表示现在状态是i,先手-后手的分数. #include<cstdio> #include<cstring> #includ ...