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消费这个消息队列,故系统重启之后,保存或者堆积在队列中的消息丢失。




原文:https://www.jianshu.com/p/93486a604c34
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值