当前位置:网站首页>Google eventbus usage details
Google eventbus usage details
2022-07-05 14:13:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
EventBus yes Google.Guava News release - Subscription class library , It implements the observer design pattern , Inform the person in charge of the message through EventBus To register / Log off the observer , In the end, the person in charge of the information informs the observer to release the information .
use first maven rely on :
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
Sample code :
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);
}
}
Observer one
package com.lance.google.event.bus;
import com.google.common.eventbus.Subscribe;
/**
* Created by zhangzh on 2017/1/10.
*/
public class DataObserver1 {
/**
* Only through @Subscribe The annotated method will be registered in EventBus
* And there are and only can be 1 Parameters
*
* @param msg
*/
@Subscribe
public void func(String msg) {
System.out.println("String msg: " + msg);
}
}
Observer 2
package com.lance.google.event.bus;
import com.google.common.eventbus.Subscribe;
/**
* Created by zhangzh on 2017/1/10.
*/
public class DataObserver2 {
/**
* post() Auto boxing is not supported , Only use Integer, Out of commission int, otherwise handlersByType Of Class Would be int instead of Intege
* And the incoming int msg Parameter in post(int msg) It's packaged as Integer, Cause can't match to
*/
@Subscribe
public void func(Integer msg) {
System.out.println("Integer msg: " + msg);
}
}
Code testing
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 ====================");
// Only registered parameter types are String The method of will be called
EventBusCenter.post("post string method");
EventBusCenter.post(123);
System.out.println("============ after unregister ============");
// Cancellation observer2
EventBusCenter.unregister(observer2);
EventBusCenter.post("post string method");
EventBusCenter.post(123);
System.out.println("============ end =============");
}
}
Output results :
============ start ====================
String msg: post string method
Integer msg: 123
============ after unregister ============
String msg: post string method
============ end =============
It looks simple to use !
EventBus Attention to the use of :
1. Code readability is poor , When used in projects , from post The place of , Inquire about handle Use , Is to use ide Search services for , The problem is hard to locate , It's not as easy to query as ordinary interface calls ;
2. because EventBus Put the message queue into memory ,listener Consume this message queue , So after the system restarts , Messages saved or piled up in the queue are lost .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/149580.html Link to the original text :https://javaforall.cn
边栏推荐
- 别不服气。手机功能升级就是强
- TDengine 社区问题双周精选 | 第三期
- 清大科越冲刺科创板:年营收2亿 拟募资7.5亿
- 瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
- Recommendation number | what are interesting people looking at?
- The forked VM terminated without saying properly goodbye
- TiCDC 6.0原理之Sorter演进
- 强联通分量
- Xampp configuring multiple items
- Why do mechanical engineers I know complain about low wages?
猜你喜欢
How to introduce devsecops into enterprises?
Why do I support bat to dismantle "AI research institute"
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
Sqllab 1-6 exercise
Why do mechanical engineers I know complain about low wages?
牛客网:拦截导弹
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
基于 TiDB 场景式技术架构过程 - 理论篇
区间 - 左闭右开
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
随机推荐
清大科越冲刺科创板:年营收2亿 拟募资7.5亿
Leetcode array question brushing notes
Controller in laravel framework
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
Enjoy what you want. Zhichuang future
upload (1-6)
Blue Bridge Cup study 2022.7.5 (morning)
Xampp configuring multiple items
Use the word "new" to attract curious people
魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
PHP5下WSDL,SOAP调用实现过程
Hongmeng fourth training
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
让秒杀狂欢更从容:大促背后的数据库(下篇)
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
Introduction, installation, introduction and detailed introduction to postman!
VC开发非MFC程序内存泄漏跟踪代码
Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success
基于伯努利原理的速度监测芯片可用于天然气管道泄露检测