继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava-eventbus,而是guava-eventbus-spring-boot-starter。spring
<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所在的包变了。app
和上篇同样。dom
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。async
@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(); } }
结果ide
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,说明确实是异步的。spring-boot