Guava-EventBus使用详解

简介: 在使用ApplicationEvent和Listener快速实现业务解耦中提到了用Spring提供的观察者设计模式完成系统内部逻辑解耦。本文将介绍Google-Guava中的一种消息发布-订阅类库——EventBus。

使用ApplicationEvent和Listener快速实现业务解耦中提到了用Spring提供的观察者设计模式完成系统内部逻辑解耦。本文将介绍Google-Guava中的一种消息发布-订阅类库——EventBus

EventBus 是Google.Guava提供的消息发布-订阅类库,它实现了观察者设计模式,消息通知负责人通过EventBus去注册/注销观察者,最后由消息通知负责人给观察者发布消息

前提:在pom.xml中引入guava包

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->  
        <dependency>  
            <groupId>com.google.guava</groupId>  
            <artifactId>guava</artifactId>  
            <version>19.0</version>  
        </dependency>  

demo

(1)EventBusCenter.java

package com.lance.google.event.bus;  
  
import com.google.common.eventbus.EventBus;  
  
/** 
 * Created by zhangzh on 2017/1/10. 
 */  
public class EventBusCenter {  
  
    private static EventBus eventBus = new EventBus();  
  
    private EventBusCenter() {  
  
    }  
  
    public static EventBus getInstance() {  
        return eventBus;  
    }  
  
    public static void register(Object obj) {  
        eventBus.register(obj);  
    }  
  
    public static void unregister(Object obj) {  
        eventBus.unregister(obj);  
    }  
  
    public static void post(Object obj) {  
        eventBus.post(obj);  
    }  
 
}  

(2) 观察者1

package com.lance.google.event.bus;  
  
import com.google.common.eventbus.Subscribe;  
  
/** 
 * Created by zhangzh on 2017/1/10. 
 */  
public class DataObserver1 {  
  
    /** 
     * 只有通过@Subscribe注解的方法才会被注册进EventBus 
     * 而且方法有且只能有1个参数 
     * 
     * @param msg 
     */  
    @Subscribe  
    public void func(String msg) {  
        System.out.println("String msg: " + msg);  
    }  
  
}  

(3) 观察者2

package com.lance.google.event.bus;  
  
import com.google.common.eventbus.Subscribe;  
  
/** 
 * Created by zhangzh on 2017/1/10. 
 */  
public class DataObserver2 {  
    /** 
     * post() 不支持自动装箱功能,只能使用Integer,不能使用int,否则handlersByType的Class会是int而不是Intege 
     * 而传入的int msg参数在post(int msg)的时候会被包装成Integer,导致无法匹配到 
     */  
    @Subscribe  
    public void func(Integer msg) {  
        System.out.println("Integer msg: " + msg);  
    }  
}  

(4) Test.java

package com.lance.google.event.bus;  
  
/** 
 * Created by zhangzh on 2017/1/10. 
 */  
public class Test {  
  
    public static void main(String[] args) throws InterruptedException {  
  
        DataObserver1 observer1 = new DataObserver1();  
        DataObserver2 observer2 = new DataObserver2();  
  
        EventBusCenter.register(observer1);  
        EventBusCenter.register(observer2);  
  
        System.out.println("============   start  ====================");  
  
        // 只有注册的参数类型为String的方法会被调用  
        EventBusCenter.post("post string method");  
        EventBusCenter.post(123);  
  
        System.out.println("============ after unregister ============");  
        // 注销observer2  
        EventBusCenter.unregister(observer2);  
        EventBusCenter.post("post string method");  
        EventBusCenter.post(123);  
  
        System.out.println("============    end           =============");  
    }  
}  

输出结果:

String msg: post string method  
Integer msg: 123  
============ after unregister ============  
String msg: post string method  
============    end           =============  
  • EventBus的使用注意问题:
  1. 代码可读性很差,项目中使用的时候,从post的地方,查询handle使用,都是使用ide的搜索服务,问题很难定位,不如普通的接口调用方便查询;
  2. 由于EventBus是将消息队列放入到内存中的,listener消费这个消息队列,故系统重启之后,保存或者堆积在队列中的消息丢失。
相关文章
|
1月前
|
安全 Android开发
你是否了解 RxJava 的 Disposable ?
你是否了解 RxJava 的 Disposable ?
131 0
|
7月前
|
存储 缓存 Java
Google Guava EventBus使用
Google Guava EventBus使用
52 0
RxJava2 中 doFinally 和 doAfterTerminate 的比较
RxJava2 中 doFinally 和 doAfterTerminate 的比较
267 0
|
监控 Java
Guava 的异步回调介绍
Guava 是非阻塞的异步回调,调用线程是不阻塞的,可以继续执行自己的业务逻辑。
519 0
|
安全 Android开发
详解 RxJava 的 Disposable
RxJava2 的 Disposable,可以在适当时机取消订阅、截断数据流,避免 Android 中的内存泄露。
1052 0
|
Java Go Android开发
RxJava2
函数式编程是一种编程范式。我们常见的编程范式有命令式编程、函数式编程和逻辑式编程。我们常见的面向对象编程是一种命令式编程。命令式编程是面向计算机硬件的抽象,有变量、赋值语句、表达式和控制语句。而函数式编程是面向数学的抽象,将计算描述为一种表达式求值,函数可以在任何地方定义,并且可以对函数进行组合。响应式编程是一种面向数据流和变化传播的编程范式,数据更新是相关联的。把函数式编程里的一套思路和响应式编程合起来就是函数响应式编程。函数响应式编程可以极大地简化项目,特别是处理嵌套回调的异步事件、复杂的列表过滤和变换或者时间相关问题。在Android开发中使用函数响应式编程的主要有两大框架:
153 0
RxJava2
|
设计模式 存储 Java
XTask与RxJava的使用对比
XTask与RxJava的使用对比
141 0
XTask与RxJava的使用对比
|
Java API
RxJava 之 ParallelFlowable
RxJava 之 ParallelFlowable
234 0
RxJava 之 ParallelFlowable
|
负载均衡 算法 Java
RxJava 并行操作
RxJava 并行操作
393 0
RxJava 并行操作
EventBus简单使用
EventBus简单使用
162 0