简介
继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava-eventbus,而是guava-eventbus-spring-boot-starter。
使用
1.引入pom
1 2 3 4 5 |
<dependency> <groupId>org.zalando.stups</groupId> <artifactId>spring-boot-starter-guava-eventbus</artifactId> <version>0.5.4</version> </dependency> |
2.MessagePublisher
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@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..."); } } |
3.EventListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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所在的包变了。
3.MessageEvent
和上篇一样。
4.测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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(); } } |
5.运行结果
1 2 |
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... |
6.使用EventBusSupport
看到Support就应该知道是啥意思了,比方说JdbcDaoSupport是帮助我们快捷使用jdbc,EventBusSupport可以帮助我们快捷使用EventBus,看下它的源码,很明显还有一个异步的方法。
1 2 3 4 5 6 7 |
public interface EventBusSupport { void post(Object event); void postAsync(Object event); } |
再看下它的实现类,可以看到是在配置类EventBusAutoConfiguration里的静态内部类EventBusSupportImpl,可以看到EventBusSupportImpl的内容其实就和我们一开使写的东西是一样的,也就是它帮我们封装好了,我们直接用它就可以了。可以看到接口里的postAsync其实就是用的EventBus的AsyncEventBus。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
@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); } } } |
7.EventBusHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@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"); } } |
8.运行测试类
1 2 3 4 5 6 7 8 9 10 11 12 |
@RunWith(SpringRunner.class) @SpringBootTest public class EventbusApplicationTests { @Autowired private EventBusHandler eventBusHandler; @Test public void contextLoads() { eventBusHandler.eventPost(); } } |
结果
1 2 3 4 |
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
本文系本人原创,如要转载,请注明出处!
0
我偶有个孤独无德的