SpringBoot+EventBus使用教程(二)

簡介

繼續上篇,本篇文章介紹如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已經不叫spring-boot-starter-guava-eventbus,而是guava-eventbus-spring-boot-starter。spring

使用

1.引入pom

        <dependency>
            <groupId>org.zalando.stups</groupId>
            <artifactId>spring-boot-starter-guava-eventbus</artifactId>
            <version>0.5.4</version>
        </dependency>

2.MessagePublisher

@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

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

3.MessageEvent

和上篇同樣。dom

4.測試類

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.運行結果

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,看下它的源碼,很明顯還有一個異步的方法。異步

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);
        }
    }
}

7.EventBusHandler

@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.運行測試類

@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

相關文章
相關標籤/搜索