当前位置:网站首页>Google EventBus 使用详解
Google EventBus 使用详解
2022-07-05 13:54:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
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 method
Integer msg: 123
============ after unregister ============
String msg: post string method
============ end =============看使用起来简单吧!
EventBus的使用注意问题:
1.代码可读性很差,项目中使用的时候,从post的地方,查询handle使用,都是使用ide的搜索服务,问题很难定位,不如普通的接口调用方便查询;
2.由于EventBus是将消息队列放入到内存中的,listener消费这个消息队列,故系统重启之后,保存或者堆积在队列中的消息丢失。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149580.html原文链接:https://javaforall.cn
边栏推荐
- LeetCode_3(无重复字符的最长子串)
- Detailed explanation of IP address and preparation of DOS basic commands and batch processing
- 金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功
- poi设置列的数据格式(有效)
- Getting started with rce
- Matlab learning 2022.7.4
- Why do I support bat to dismantle "AI research institute"
- Wonderful express | Tencent cloud database June issue
- Redis6 data type and operation summary
- 关于Apache Mesos的一些想法
猜你喜欢

我为什么支持 BAT 拆掉「AI 研究院」

How to deal with the Yellow Icon during the installation of wampserver
Jetpack compose introduction to mastery

荐号 | 有趣的人都在看什么?
![[cloud resources] what software is good for cloud resource security management? Why?](/img/c2/85d6b4a956afc99c2dc195a1ac3938.png)
[cloud resources] what software is good for cloud resource security management? Why?

基于微信小程序的订餐系统

zabbix 监控

Summit review | baowanda - an integrated data security protection system driven by compliance and security
![[public class preview]: basis and practice of video quality evaluation](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[public class preview]: basis and practice of video quality evaluation

神经网络物联网未来现状和趋势及看法
随机推荐
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
荐号 | 有趣的人都在看什么?
Jetpack Compose入门到精通
Rk3566 add LED
Brief introduction to revolutionary neural networks
Network security - Novice introduction
Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
Those things I didn't know until I took the postgraduate entrance examination
OSI and tcp/ip protocol cluster
蓝桥杯学习2022.7.5(上午)
Redis6 transaction and locking mechanism
Request + BS4 crawl Netease cloud music popular comments
神经网络物联网未来现状和趋势及看法
How to divide a large 'tar' archive file into multiple files of a specific size
SSH免密码登录详解
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
【云资源】云资源安全管理用什么软件好?为什么?
广发期货排名多少?网上办理广发期货开户安全可靠吗?
Requset + BS4 crawling shell listings
[machine learning notes] how to solve over fitting and under fitting