当前位置:网站首页>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
边栏推荐
- 01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
- 神经网络物联网未来现状和趋势及看法
- Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
- 登录界面代码
- What is the future development trend of neural network Internet of things
- How to call the function mode of one hand and one machine
- C - Divisors of the Divisors of An Integer Gym - 102040C
- Login interface code
- 不相交集
- 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)
猜你喜欢

Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan

牛客网:拦截导弹

What are the advantages and characteristics of SAS interface

Redis如何实现多可用区?

Detailed explanation of IP address and preparation of DOS basic commands and batch processing

如何将 DevSecOps 引入企业?
![[machine learning notes] several methods of splitting data into training sets and test sets](/img/f6/eca239bb4b1764a1495ccd9a868ec1.jpg)
[machine learning notes] several methods of splitting data into training sets and test sets

Current situation, trend and view of neural network Internet of things in the future

IP packet header analysis and static routing

魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
随机推荐
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
What is the future development trend of neural network Internet of things
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
鸿蒙第四次培训
Assembly language
判断变量是否为数组
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
最长公共子序列 - 动态规划
Brief introduction to revolutionary neural networks
03_Solr之dataimport
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
Show strength. In this way, the mobile phone will not be difficult to move forward
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
Introduction, installation, introduction and detailed introduction to postman!
TiFlash 面向编译器的自动向量化加速
Judge whether the variable is an array
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting