当前位置:网站首页>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
边栏推荐
- 3W原则[通俗易懂]
- 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
- 强联通分量
- 为什么我认识的机械工程师都抱怨工资低?
- Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
- Google EventBus 使用详解
- 最简单不用证书也可以多开功能的方式
- 牛客网:拦截导弹
- Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
- Simple process of penetration test
猜你喜欢

物联网应用技术专业是属于什么类

What category does the Internet of things application technology major belong to

What is the future development trend of neural network Internet of things

Why do I support bat to dismantle "AI research institute"

Simple process of penetration test

金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功

IP packet header analysis and static routing

Tdengine biweekly selection of community issues | phase III

治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东

Brief introduction to revolutionary neural networks
随机推荐
无密码身份验证如何保障用户隐私安全?
Hongmeng fourth training
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
IP packet header analysis and static routing
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
Current situation, trend and view of neural network Internet of things in the future
鏈錶(簡單)
04_solr7.3之solrJ7.3的使用
怎么叫一手一机的功能方式
Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu
Linked list (simple)
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
3W原则[通俗易懂]
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
[buuctf.reverse] 152-154
WebRTC的学习(二)
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
Judge whether the variable is an array
Routing in laravel framework
Discussion on memset assignment