Google EventBus 使用详解
来源:互联网 发布:一直在启动windows 编辑:程序博客网 时间:2024/05/17 02:30
EventBus是Google.Guava提供的消息发布-订阅类库,它实现了观察者设计模式,消息通知负责人通过EventBus去注册/注销观察者,最后由消息通知负责人给观察者发布消息。
首先使用 maven 依赖:
<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
示例代码:
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); }}
观察者一
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); }}
观察者二
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); }}
代码测试
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 ============="); }}
输出结果:
============ start ====================String msg: post string methodInteger msg: 123============ after unregister ============String msg: post string method============ end =============
看使用起来简单吧!
0 0
- Google EventBus 使用详解
- Google EventBus 使用详解
- EventBus使用详解
- EventBus使用详解
- EventBus使用详解
- EventBus使用详解
- EventBus使用详解
- EventBus 使用详解
- EventBus使用详解
- EventBus使用详解
- Android EventBus 使用详解
- Android EventBus使用详解
- EventBus的使用详解
- EventBus 3.0 使用详解
- EventBus使用详解(一)
- EventBus使用详解(二)
- EventBus 3.0使用详解
- EventBus使用详解
- windwos下安装RabbitMQ
- 微信小程序示例开发
- 龟兔赛跑预测
- Base64编码
- Fedora-Workstation-Live-x86_64-25-1.3环境下安装Linux64位版本的DrClient
- Google EventBus 使用详解
- Parasoft C++test使用教程:测试用例生成(上)
- hdu 1372 Knight Moves
- CSS3布局 —— Columns
- CXF系列(五):soap与rest的比较
- H3 BPM Sheet表单 NTKO功能使用说明
- ubuntu12.04安装opencv3.2.0
- 误删除VMware虚拟机vmdk文件的恢复方法
- MULE 连接sap rfc接口开发二:调用rfc接口