继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava-eventbus,而是guava-eventbus-spring-boot-starter。
<dependency>
<groupId>org.zalando.stups</groupId>
<artifactId>spring-boot-starter-guava-eventbus</artifactId>
<version>0.5.4</version>
</dependency>
@Component
@Slf4j public class MessagePublisher { private final EventBus eventBus;
@Autowired public MessagePublisher(final EventBus eventBus){ this.eventBus = eventBus;
} public void sendMessage(){ this.eventBus.post(MessageEvent.builder().id(1).name("test").build());
log.info("send message...");
}
}
import com.google.common.eventbus.Subscribe; import com.sww.eventbus.domain.MessageEvent; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component;
@Component
@Slf4j public class EventListener {
@Subscribe public void onMessageEvent(MessageEvent event) {
log.info("Subscribe message:{}", event);
}
}
这边和上篇不一样的是@Subscribe所在的包变了。
和上篇一样。
import com.sww.eventbus.publish.MessagePublisher; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest public class EventbusApplicationTests {
@Autowired private MessagePublisher messagePublisher;
@Test public void contextLoads() {
messagePublisher.sendMessage();
}
}
2019-11-03 20:32:25.052 INFO 16172 --- [ main] com.sww.eventbus.listener.EventListener : Subscribe message:MessageEvent(id=1, name=test)
2019-11-03 20:32:25.052 INFO 16172 --- [ main] c.sww.eventbus.publish.MessagePublisher : send message...
看到Support就应该知道是啥意思了,比方说JdbcDaoSupport是帮助我们快捷使用jdbc,EventBusSupport可以帮助我们快捷使用EventBus,看下它的源码,很明显还有一个异步的方法。
public interface EventBusSupport { void post(Object event); void postAsync(Object event);
}
再看下它的实现类,可以看到是在配置类EventBusAutoConfiguration里的静态内部类EventBusSupportImpl,可以看到EventBusSupportImpl的内容其实就和我们一开使写的东西是一样的,也就是它帮我们封装好了,我们直接用它就可以了。可以看到接口里的postAsync其实就是用的EventBus的AsyncEventBus。
@Configuration public class EventBusAutoConfiguration {
@Bean public EventBusSupport eventBusWrapper() { return new EventBusSupportImpl(eventBus(), asyncEventBus());
}
@Bean public EventBus eventBus() {
EventBus eventBus = new EventBus(); return eventBus;
}
@Bean public AsyncEventBus asyncEventBus() {
AsyncEventBus asyncEventBus = new AsyncEventBus("asyncDefault", Executors.newFixedThreadPool(2)); return asyncEventBus;
}
@Bean public EventBusSubscriberBeanPostProcessor subscriberAnnotationProcessor() { return new EventBusSubscriberBeanPostProcessor(eventBus(), asyncEventBus());
} /** \* Simple implementation of {@link EventBusSupport}.
\*
\* @author jbellmann */
static final class EventBusSupportImpl implements EventBusSupport { private EventBus eventBus; private AsyncEventBus asyncEventBus;
EventBusSupportImpl(final EventBus eventBus, final AsyncEventBus asyncEventBus) {
Assert.notNull(eventBus, "EventBus should not be null");
Assert.notNull(asyncEventBus, "AsyncEventBus should not be null"); this.eventBus = eventBus; this.asyncEventBus = asyncEventBus;
}
@Override public void post(final Object event) { this.eventBus.post(event);
}
@Override public void postAsync(final Object event) { this.asyncEventBus.post(event);
}
}
}
@Component
@Slf4j public class EventBusHandler {
@Autowired private final EventBusSupport eventBusSupport; public EventBusHandler(final EventBusSupport eventBusSupport){ this.eventBusSupport = eventBusSupport;
} public void eventPost(){
eventBusSupport.post(MessageEvent.builder().id(1).name("test").build());
log.info("post event");
eventBusSupport.postAsync(MessageEvent.builder().id(2).name("AsyncTest").build());
log.info("post async event");
}
}
@RunWith(SpringRunner.class)
@SpringBootTest public class EventbusApplicationTests {
@Autowired private EventBusHandler eventBusHandler;
@Test public void contextLoads() {
eventBusHandler.eventPost();
}
}
结果
2019-11-03 20:50:02.028 INFO 12292 --- [ main] com.sww.eventbus.listener.EventListener : Subscribe message:MessageEvent(id=1, name=test) 2019-11-03 20:50:02.028 INFO 12292 --- [ main] c.sww.eventbus.publish.EventBusHandler : post event 2019-11-03 20:50:02.044 INFO 12292 --- [ main] c.sww.eventbus.publish.EventBusHandler : post async event 2019-11-03 20:50:02.044 INFO 12292 --- [pool-1-thread-1] com.sww.eventbus.listener.EventListener : Subscribe message:MessageEvent(id=2, name=AsyncTest)
可以看到AsyncTest的线程是pool-1-thread-1,而不是main,说明确实是异步的。
https://download.csdn.net/download/u013081610/11971245
原网址: 访问
创建于: 2021-01-21 15:49:45
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论