`
sunxboy
  • 浏览: 2887822 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Google Guava EventBus

 
阅读更多

在Google Guava 10版本引入了EventBus, 它主要用来简化我们处理生产/消费者编程模型. 

基本用法 
使用Guava之后, 如果要订阅消息, 就不用再继承指定的接口, 只需要在指定的方法上加上@Subscribe注解即可: 

Java代码  收藏代码
  1. public class EventListener {  
  2.    
  3.     public int lastMessage = 0;  
  4.    
  5.     @Subscribe  
  6.     public void listen(OurTestEvent event) {  
  7.         lastMessage = event.getMessage();  
  8.     }  
  9.    
  10.     public int getLastMessage() {  
  11.         return lastMessage;  
  12.     }  
  13. }  



上面的lastMessage用来接收消息. 

下面定义的类用来对消息进行封装: 

Java代码  收藏代码
  1. public class OurTestEvent {  
  2.    
  3.     private final int message;  
  4.    
  5.     public OurTestEvent(int message) {  
  6.         this.message = message;  
  7.     }  
  8.    
  9.     public int getMessage() {  
  10.         return message;  
  11.     }  
  12. }  



通过写一个测试来了解EventBus如何工作: 

Java代码  收藏代码
  1. @Test  
  2. public void shouldReceiveEvent() throws Exception {  
  3.    
  4.     // given  
  5.     EventBus eventBus = new EventBus("test");  
  6.     EventListener listener = new EventListener();  
  7.    
  8.     eventBus.register(listener);  
  9.    
  10.     // when  
  11.     eventBus.post(new OurTestEvent(200));  
  12.    
  13.     // then  
  14.     assertThat(listener.getLastMessage()).isEqualTo(200);  
  15. }  



上面的测试是不是很简单? 

MultiListener的使用 

只需要在要订阅消息的方法上加上@Subscribe注解即可实现对多个消息的订阅: 

Java代码  收藏代码
  1. public class MultipleListener {  
  2.    
  3.     public Integer lastInteger;  
  4.     public Long lastLong;  
  5.    
  6.     @Subscribe  
  7.     public void listenInteger(Integer event) {  
  8.         lastInteger = event;  
  9.     }  
  10.    
  11.     @Subscribe  
  12.     public void listenLong(Long event) {  
  13.         lastLong = event;  
  14.     }  
  15.    
  16.     public Integer getLastInteger() {  
  17.         return lastInteger;  
  18.     }  
  19.    
  20.     public Long getLastLong() {  
  21.         return lastLong;  
  22.     }  
  23. }  



下面是对应的测试: 

Java代码  收藏代码
  1. @Test  
  2. public void shouldReceiveMultipleEvents() throws Exception {  
  3.    
  4.     // given  
  5.     EventBus eventBus = new EventBus("test");  
  6.     MultipleListener multiListener = new MultipleListener();  
  7.    
  8.     eventBus.register(multiListener);  
  9.    
  10.     // when  
  11.     eventBus.post(new Integer(100));  
  12.     eventBus.post(new Long(800));  
  13.    
  14.     // then  
  15.     assertThat(multiListener.getLastInteger()).isEqualTo(100);  
  16.     assertThat(multiListener.getLastLong()).isEqualTo(800L);  
  17. }  



高级用法 

1.Dead Event 

如果EventBus发送的消息都不是订阅者关心的称之为Dead Event. 看下面的例子: 

Java代码  收藏代码
  1. /** 
  2.  * Listener waiting for the event that any message was posted but not delivered to anyone 
  3.  */  
  4. public class DeadEventListener {  
  5.    
  6.     boolean notDelivered = false;  
  7.    
  8.     @Subscribe  
  9.     public void listen(DeadEvent event) {  
  10.         notDelivered = true;  
  11.     }  
  12.    
  13.     public boolean isNotDelivered() {  
  14.         return notDelivered;  
  15.     }  
  16. }  



下面是测试类: 

Java代码  收藏代码
  1. @Test  
  2. public void shouldDetectEventWithoutListeners() throws Exception {  
  3.    
  4.     // given  
  5.     EventBus eventBus = new EventBus("test");  
  6.    
  7.     DeadEventListener deadEventListener = new DeadEventListener();  
  8.     eventBus.register(deadEventListener);  
  9.    
  10.     // when  
  11.     eventBus.post(new OurTestEvent(200));  
  12.    
  13.     assertThat(deadEventListener.isNotDelivered()).isTrue();  
  14. }  



如果没有消息订阅者监听消息, EventBus将发送DeadEvent消息, 这时我们可以通过log的方式来记录这种状态. 

2.Event的继承 

如果Listener A监听Event A, 而Event A有一个子类Event B, 此时Listener A将同时接收Event A和B消息 

看下面的例子: 

Java代码  收藏代码
  1. public class NumberListener {  
  2.    
  3.     private Number lastMessage;  
  4.    
  5.     @Subscribe  
  6.     public void listen(Number integer) {  
  7.         lastMessage = integer;  
  8.     }  
  9.    
  10.     public Number getLastMessage() {  
  11.         return lastMessage;  
  12.     }  
  13. }  
  14.   
  15.       
  16. public class IntegerListener {  
  17.    
  18.     private Integer lastMessage;  
  19.    
  20.     @Subscribe  
  21.     public void listen(Integer integer) {  
  22.         lastMessage = integer;  
  23.     }  
  24.    
  25.     public Integer getLastMessage() {  
  26.         return lastMessage;  
  27.     }  
  28. }  



对应的测试类: 


Java代码  收藏代码
  1. @Test  
  2. public void shouldGetEventsFromSubclass() throws Exception {  
  3.    
  4.     // given  
  5.     EventBus eventBus = new EventBus("test");  
  6.     IntegerListener integerListener = new IntegerListener();  
  7.     NumberListener numberListener = new NumberListener();  
  8.     eventBus.register(integerListener);  
  9.     eventBus.register(numberListener);  
  10.    
  11.     // when  
  12.     eventBus.post(new Integer(100));  
  13.    
  14.     // then  
  15.     assertThat(integerListener.getLastMessage()).isEqualTo(100);  
  16.     assertThat(numberListener.getLastMessage()).isEqualTo(100);  
  17.    
  18.     //when  
  19.     eventBus.post(new Long(200L));  
  20.    
  21.     // then  
  22.     // this one should has the old value as it listens only for Integers  
  23.     assertThat(integerListener.getLastMessage()).isEqualTo(100);  
  24.     assertThat(numberListener.getLastMessage()).isEqualTo(200L);  
  25. }  

 

分享到:
评论

相关推荐

    Guava-Event-Bus:Guava的EventBus源码学习

    EventBus是google的一个Java工具包其中的一个工具类,类似的有多个版本其中包括移植到Android端的 和改良的,功能基本都是一样的。此处主要是对Guava版的EventBus分析,关于。 ###2. 功能简介 ####2.1 EventBus介绍 ...

    不加密Google Guava视频教程.txt

    ├─Google Guava 第19讲-Guava之EventBus和NIO2.0 WatchService综合实战.wmv ├─Google Guava 第20讲-实战:手动实现一个EventBus-01快速搭建程序结构.wmv ├─Google Guava 第21讲-实战:手动实现一个EventBus-...

    Google Guava 官方教程

    EventBus 是 Guava 中的一个轻量级事件总线,用于组件间的解耦通信。订阅者可以注册对特定事件的兴趣,当事件发生时,会被通知。 通过学习 Google Guava 官方教程,开发者能够更有效地利用这些工具,提高代码质量...

    EventBus与Spring Event区别详解(EventBus 事件机制,Spring Event事件机制)

    EventBus是Google Guava项目的一部分,提供了一个轻量级的事件机制实现。它基于观察者模式(或发布订阅模式),主要分为三部分:发布者、监听者、事件。EventBus的使用非常简单,只需要定义事件对象、事件监听器和...

    guava_programming.zip

    《深入理解Google Guava:函数式接口与EventBus解析》 Google Guava是Google推出的一个开源库,它为Java开发人员提供了一套丰富的工具类和集合框架,极大地提高了开发效率和代码质量。本资料主要围绕Guava中的两个...

    google guava 中文教程

    Guava还包含了一些其他实用功能,如事件总线(EventBus)用于解耦组件间的通信,RangeSet用于处理有序值的集合,Optional类用于表示可能为空的值,避免空指针异常。 总的来说,Google Guava库是一个功能强大的Java...

    google-guava.jar

    《谷歌Guava库详解》 谷歌Guava,全称为Google Guava,是Google推出的一款Java库,它包含一系列基础工具类,旨在简化Java开发工作,提高代码效率和可维护性。Guava的核心特性包括集合框架、缓存、原始类型支持、...

    guava-18.0(guava-18.0.jar和guava-18.0-sources.jar)

    Guava是Google开发的一个核心库,它为Java平台提供了许多实用工具类,涵盖了集合、并发、I/O、字符串处理、数学运算等多个方面。这个压缩包包含的是Guava库的18.0版本,分为两个部分:`guava-18.0.jar`和`guava-18.0...

    Google Guava 30.1.1常用类介绍及实践代码

    以上只是 Guava 30.1.1 版本中的一部分核心功能,实际使用中还有许多其他实用工具类,如 `Range`, `EventBus`, `RateLimiter` 等。Guava 的设计目标是提高代码质量和效率,通过提供这些工具类,开发者可以更加专注于...

    Getting Started with Google Guava

    9. **事件监听**:Guava的`EventBus`是一个简单的发布-订阅事件总线,用于组件间的通信,使得松耦合的组件能有效地传递信息。 10. **服务加载器**:Guava的`ServiceLoader`可以方便地加载实现特定接口的类,特别是...

    Guava的Jar包(guava-26.0-jre.jar)

    Guava是Google公司开发的一款适用于Java的工具类集合。

    Guava 工程项目包 有实例

    Guava 是一个由 Google 开发并维护的 Java 库,它提供了许多实用工具类和集合框架的增强功能,极大地丰富了 Java 平台的标准库。Guava 的目标是解决 Java 开发人员在日常工作中遇到的各种常见问题,提高开发效率和...

    SpringBoot+EventBus使用教程示例代码

    EventBus是Google Guava库的一部分,它提供了一种发布/订阅模式的事件传递机制,允许应用程序的不同组件之间进行异步通信,而无需直接引用彼此。 **二、EventBus的使用** 1. **依赖添加**:首先,在SpringBoot项目...

    Guava常用类库 v33.0.0.zip

    Guava是Google开发的一个Java库,它包含许多Google核心库中的高级集合、缓存、原生类型支持、并发包、字符串处理、I/O等工具类。版本33.0.0是Guava的一个更新,提供了最新的功能改进和错误修复。在深入探讨Guava常用...

    guava-26.0-jre.zip

    Guava是Google为Java平台设计并维护的一个开源库,它提供了许多高级功能和实用工具,极大地增强了Java开发的效率和代码质量。Guava 26.0-jre版本是针对Java运行时环境(JRE)优化的一个特定版本,旨在更好地支持Java...

    google guava23.5

    谷歌Guava库是Java开发人员广泛使用的开源工具集,它提供了许多高级数据结构、集合类、缓存机制、并发工具、I/O工具以及字符串处理功能等。Guava 23.5是该库的一个特定版本,它包含了从早期版本到23.5之间的一系列...

    guava14.0.1jar包

    8. **事件监听**:Guava的EventBus是一种发布-订阅事件总线,用于组件之间的松耦合通信,可以替代传统的监听器模式。 9. ** Predicates和Conditions**:Guava提供了预定义和自定义的谓词(Predicate)和条件...

    AsmEventBus:AsmEventBus 是带有 asm 的事件系统的实现。 (废话...)

    它比 Google Guava的EventBus更快、更强大 {citation needed}。 什么是 ASM ASM是一个java库,提供实时的字节码修改和生成。 它的名字被称为内联汇编器, 虽然我认为是沙明丸绫的迷你裙…… AsmEventBus由ASM供电...

    guava-jdk5

    8. **事件监听**:Guava的 EventBus 使得事件驱动编程在Java 5环境下变得可能,它允许组件之间松散耦合,通过发布和订阅事件来进行通信。 9. **运行时类型信息**:Guava提供了TypeToken和Types等工具类,帮助开发者...

    guava-18.0资料

    Guava是Google开发的一个核心库,它为Java平台提供了许多实用工具类,极大地丰富了Java标准库的功能。这个"guava-18.0资料"包含的资源是Guava库的18.0版本,该版本是Guava的一个稳定版本,提供了一系列改进和新特性...

Global site tag (gtag.js) - Google Analytics