当前位置:网站首页>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
边栏推荐
- 故障分析 | MySQL 耗尽主机内存一例分析
- 关于Apache Mesos的一些想法
- 如何深入理解“有限状态机”的设计思想?
- poi设置列的数据格式(有效)
- Redis如何实现多可用区?
- Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
- After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
- Lepton 无损压缩原理及性能分析
- 汇编语言 assembly language
- Fault analysis | analysis of an example of MySQL running out of host memory
猜你喜欢
Lepton 无损压缩原理及性能分析
为什么我认识的机械工程师都抱怨工资低?
LeetCode_2(两数相加)
What category does the Internet of things application technology major belong to
tidb-dm报警DM_sync_process_exists_with_error排查
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
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
Why do mechanical engineers I know complain about low wages?
What are the advantages and characteristics of SAS interface
循环不变式
随机推荐
Request + BS4 crawl Netease cloud music popular comments
关于memset赋值的探讨
Comparison of several distributed databases
Mysql database installation tutorial under Linux
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
How to call the function mode of one hand and one machine
牛客网:拦截导弹
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
Implementation process of WSDL and soap calls under PHP5
最简单不用证书也可以多开功能的方式
神经网络物联网未来发展趋势怎么样
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)
治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
广发期货排名多少?网上办理广发期货开户安全可靠吗?
The forked VM terminated without saying properly goodbye
金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功
汇编语言 assembly language
The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
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