通过eventBus.register注册订阅者,通过eventBus.post方法发布事件,然后根据发布事件的类型(classType),执行所有订阅者中被@Subcribe注解标记的且参数类型一致的方法,从而实现发布、订阅功能。 源码解读: 源码基于如下版本解析: 1 2 3 4 5 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</arti...
importcom.google.common.eventbus.EventBus;importorg.springframework.stereotype.Service;/** * 事件分发器,使用google guava EventBus机制 */@ServicepublicclassEventDispatcherImplimplementsEventDispatcher{privateEventBuseventBus=newEventBus();@Overridepublicvoidregister(Objectobject){eventBus.register(object);}@...
创建EventBus 对象。通常全局或模块内通过单例模式只用一个 EventBus 对象 创建消息类 创建监听者类 注册监听者类。如果有多个 EventBus 对象,监听者类注册在哪个 EventBus 对象下,消息就需要发布到对应的 EventBus 中 发布消息 EventBusDemo.java packagecn.jkingtools.demo.guava.eventbus; importcom.google.comm...
3. 通过eventBus.post()方法发布事件,根据事件类型找到所有对应subcriber,遍历;然后由eventBus中的线程池执行订阅者中入参类型匹配的方法。 简单实用示例: 在springboot中,使用自动注入的方式来实现 目录结构: 代码如下: package com.example.demo.eventbus.config; import com.google.common.eventbus.EventBus; impo...
Google Guava EventBus 在Google Guava 10版本引入了EventBus, 它主要用来简化我们处理生产/消费者编程模型. 基本用法 使用Guava之后, 如果要订阅消息, 就不用再继承指定的接口, 只需要在指定的方法上加上@Subscribe注解即可: 1. public class 2. 3. public int lastMessage = 0;...
结论:Google guava中的AsyncEventBus组件默认是同步处理,使用多线程但未设置@AllowConcurrentEvents会出现多线程之间的抢锁情况,导致业务消费kafka消息变慢产生消息积压。 四、解决方案 方案一:去除EventBus这一层,直接使用主线程消费EventBus使用场景一般在有多个订阅者情况下使用,排查业务逻辑过程中业务逻辑只有一个Subscri...
guava OverridePredicate.testwherever we overridePredicate.apply. Feb 28, 2025 integration-tests/gradle Remove dependency on Checker Framework annotations. Dec 22, 2024 proguard Use most of the mainAbstractFutureimplementation from J2KT and fr…
Google core libraries for Java. Contribute to google/guava development by creating an account on GitHub.
For example, when a pupil enters to the bus, home knows that it had one occupant less. From the point of Google Guava, event bus is a lightweight system that can be used to event distribution program. This main advantage is the flexibility. The components don't need to know each other...
在Spring cloud环境下,使用google公司开源的guava工具类EventBus。 一、引入guava的jar包 二、在config下新建一个类EventBusConfig.java importcom.google.common.eventbus.AsyncEventBus;importcom.google.common.eventbus.Subscribe;importorg.reflections.Reflections;importorg.reflections.scanners.MethodAnnotationsScanner;...