Guava中的EventBus使用笔记
物料准备:
1.引入guava依赖
2.定义一个EventBusUtil工具类
3.定义自定义的事件以及监听逻辑
4.测试发布事件效果
引入guava依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
定义一个EventBusUtil工具类
package com.example.demo.service.guava;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import java.util.concurrent.Executor;
public class EventBusUtil {
private static EventBus eventBus;
private static AsyncEventBus asyncEventBus;
private static Executor executor = new Executor() {
public void execute(Runnable command) {
new Thread(command).start();
}
};
//双重锁单例模式
private static AsyncEventBus getAsynEventBus(){
if(asyncEventBus==null){
synchronized (AsyncEventBus.class){
if(asyncEventBus==null){
asyncEventBus = new AsyncEventBus(executor);
}
}
}
return asyncEventBus;
}
//双重锁单例模式
private static EventBus getEventBus(){
if(eventBus==null){
synchronized (EventBus.class){
if(eventBus==null){
eventBus = new EventBus();
}
}
}
return eventBus;
}
public static void post(Object event){
getEventBus().post(event);
}
//异步方式发送事件
public static void asyncPost(Object event){
getAsynEventBus().post(event);
}
public static void registerEventBus(Object object){
getEventBus().register(object);
}
public static void registerAsynEventBus(Object object){
getAsynEventBus().register(object);
}
}
自定义事件类
public class CustomEvent {
private int age;
public CustomEvent(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
}
自定义监听逻辑
package com.example.demo.service.guava;
import com.google.common.eventbus.Subscribe;
import org.springframework.stereotype.Component;
import java.time.Instant;
@Component
public class EventListener1 {
@Subscribe
public void test1(CustomEvent event){
System.out.println(Instant.now() +"监听者1-->订阅者1,收到事件:"+event.getAge()+",线程号为:"+Thread.currentThread().getName());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Subscribe
public void test2(CustomEvent event){
System.out.println(Instant.now() +"监听者1-->订阅者2,收到事件:"+event.getAge()+",线程号为:"+Thread.currentThread().getName());
}
}
package com.example.demo.service.guava;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class EventListener2 {
@Subscribe
public void test7051(CustomEvent event){
log.info("test7051-evt:{}",event.getAge());
}
@Subscribe
public void test7052(CustomEvent event){
log.info("test7052-evt:{}",event.getAge());
}
@Subscribe
public void test7053(CustomEvent event){
log.info("test7053-evt:{}",event.getAge());
}
}
测试发布事件效果
测试发送普通事件(阻塞)
@Test
void test111902(){
EventListener1 listener1 = new EventListener1();
EventListener2 listener2 = new EventListener2();
EventBusUtil.registerEventBus(listener1);
EventBusUtil.registerEventBus(listener2);
CustomEvent customEvent = new CustomEvent(23);
EventBusUtil.post(customEvent);
}
[INFO] [2023-07-05 19:07:38.110] [main] com.example.demo.OtherTests.logStarted:61 --> Started OtherTests in 5.046 seconds (JVM running for 7.636)
2023-07-05T11:07:38.987Z监听者1-->订阅者2,收到事件:23,线程号为:main
2023-07-05T11:07:38.988Z监听者1-->订阅者1,收到事件:23,线程号为:main
[INFO] [2023-07-05 19:07:39.288] [main] com.example.demo.service.guava.EventListener2.test7053:40 --> test7053-evt:23
[INFO] [2023-07-05 19:07:39.288] [main] com.example.demo.service.guava.EventListener2.test7051:29 --> test7051-evt:23
[INFO] [2023-07-05 19:07:39.288] [main] com.example.demo.service.guava.EventListener2.test7052:35 --> test7052-evt:23
测试发送异步事件 (非阻塞)
@Test
void testGuavaAsyncEvt(){
EventListener2 listener2 = new EventListener2();
EventBusUtil.registerAsynEventBus(listener2);
CustomEvent customEvent = new CustomEvent(23);
CustomEvent customEvent2 = new CustomEvent(50);
EventBusUtil.asyncPost(customEvent);
EventBusUtil.asyncPost(customEvent2);
}
[INFO] [2023-07-05 19:08:59.992] [main] com.example.demo.OtherTests.logStarted:61 --> Started OtherTests in 4.951 seconds (JVM running for 7.378)
[INFO] [2023-07-05 19:09:00.474] [Thread-3] com.example.demo.service.guava.EventListener2.test7051:29 --> test7051-evt:23
[INFO] [2023-07-05 19:09:00.476] [Thread-4] com.example.demo.service.guava.EventListener2.test7053:40 --> test7053-evt:23
[INFO] [2023-07-05 19:09:00.476] [Thread-2] com.example.demo.service.guava.EventListener2.test7052:35 --> test7052-evt:23
[INFO] [2023-07-05 19:09:00.479] [Thread-6] com.example.demo.service.guava.EventListener2.test7051:29 --> test7051-evt:50
[INFO] [2023-07-05 19:09:00.481] [Thread-7] com.example.demo.service.guava.EventListener2.test7053:40 --> test7053-evt:50
[INFO] [2023-07-05 19:09:00.503] [Thread-5] com.example.demo.service.guava.EventListener2.test7052:35 --> test7052-evt:50

文章介绍了如何在Java项目中使用Guava的EventBus组件,包括设置依赖、创建EventBus实例、定义事件类和监听器方法,以及同步和异步发送事件的测试案例。通过EventBus,可以实现不同组件间的通信和解耦。
1055

被折叠的 条评论
为什么被折叠?



