当前位置:网站首页>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
边栏推荐
- Why do I support bat to dismantle "AI research institute"
- WebRTC的学习(二)
- 矩阵链乘 - 动态规划实例
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- Brief introduction to revolutionary neural networks
- VC开发非MFC程序内存泄漏跟踪代码
- 最长公共子序列 - 动态规划
- 常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...
- Assembly language
- 总量分析 核算方法和势方法 - 分摊分析
猜你喜欢

神经网络物联网未来发展趋势怎么样

upload (1-6)

Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
![Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]](/img/c2/a5f5fe17a6bd1f6f9df828ddd224d6.png)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]

金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功

IP packet header analysis and static routing
![[js] basic syntax - for loop](/img/7f/6ddc47c062caa7d39538f88e12b1a0.jpg)
[js] basic syntax - for loop

循环不变式

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】

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
随机推荐
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
tidb-dm报警DM_sync_process_exists_with_error排查
Implementation process of WSDL and soap calls under PHP5
LeetCode_3(无重复字符的最长子串)
[machine learning notes] several methods of splitting data into training sets and test sets
Leetcode array question brushing notes
Current situation, trend and view of neural network Internet of things in the future
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
鸿蒙第四次培训
Anchor navigation demo
牛客网:拦截导弹
判断变量是否为数组
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
Tiflash compiler oriented automatic vectorization acceleration
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
Getting started with rce
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
UE source code reading [1]--- starting with problems delayed rendering in UE
关于Apache Mesos的一些想法