简介

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

使用

1.引入pom

  1. <dependency>
  2. <groupId>org.zalando.stups</groupId>
  3. <artifactId>spring-boot-starter-guava-eventbus</artifactId>
  4. <version>0.5.4</version>
  5. </dependency>

2.MessagePublisher

  1. @Component
  2. @Slf4j
  3. public class MessagePublisher {
  4.  
  5. private final EventBus eventBus;
  6.  
  7. @Autowired
  8. public MessagePublisher(final EventBus eventBus){
  9. this.eventBus = eventBus;
  10. }
  11.  
  12. public void sendMessage(){
  13. this.eventBus.post(MessageEvent.builder().id(1).name("test").build());
  14. log.info("send message...");
  15. }
  16.  
  17. }

3.EventListener

  1. import com.google.common.eventbus.Subscribe;
  2. import com.sww.eventbus.domain.MessageEvent;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. @Slf4j
  8. public class EventListener {
  9.  
  10. @Subscribe
  11. public void onMessageEvent(MessageEvent event) {
  12. log.info("Subscribe message:{}", event);
  13. }
  14.  
  15. }

这边和上篇不一样的是@Subscribe所在的包变了。

3.MessageEvent

和上篇一样。

4.测试类

  1. import com.sww.eventbus.publish.MessagePublisher;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7.  
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class EventbusApplicationTests {
  11.  
  12. @Autowired
  13. private MessagePublisher messagePublisher;
  14.  
  15. @Test
  16. public void contextLoads() {
  17. messagePublisher.sendMessage();
  18. }
  19.  
  20. }

5.运行结果

  1. 2019-11-03 20:32:25.052 INFO 16172 --- [ main] com.sww.eventbus.listener.EventListener : Subscribe message:MessageEvent(id=1, name=test)
  2. 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. public interface EventBusSupport {
  2.  
  3. void post(Object event);
  4.  
  5. void postAsync(Object event);
  6.  
  7. }

再看下它的实现类,可以看到是在配置类EventBusAutoConfiguration里的静态内部类EventBusSupportImpl,可以看到EventBusSupportImpl的内容其实就和我们一开使写的东西是一样的,也就是它帮我们封装好了,我们直接用它就可以了。可以看到接口里的postAsync其实就是用的EventBus的AsyncEventBus。

  1. @Configuration
  2. public class EventBusAutoConfiguration {
  3.  
  4. @Bean
  5. public EventBusSupport eventBusWrapper() {
  6. return new EventBusSupportImpl(eventBus(), asyncEventBus());
  7. }
  8.  
  9. @Bean
  10. public EventBus eventBus() {
  11. EventBus eventBus = new EventBus();
  12. return eventBus;
  13. }
  14.  
  15. @Bean
  16. public AsyncEventBus asyncEventBus() {
  17. AsyncEventBus asyncEventBus = new AsyncEventBus("asyncDefault", Executors.newFixedThreadPool(2));
  18. return asyncEventBus;
  19. }
  20.  
  21. @Bean
  22. public EventBusSubscriberBeanPostProcessor subscriberAnnotationProcessor() {
  23. return new EventBusSubscriberBeanPostProcessor(eventBus(), asyncEventBus());
  24. }
  25.  
  26. /**
  27. * Simple implementation of {@link EventBusSupport}.
  28. *
  29. * @author jbellmann
  30. */
  31. static final class EventBusSupportImpl implements EventBusSupport {
  32. private EventBus eventBus;
  33. private AsyncEventBus asyncEventBus;
  34.  
  35. EventBusSupportImpl(final EventBus eventBus, final AsyncEventBus asyncEventBus) {
  36. Assert.notNull(eventBus, "EventBus should not be null");
  37. Assert.notNull(asyncEventBus, "AsyncEventBus should not be null");
  38. this.eventBus = eventBus;
  39. this.asyncEventBus = asyncEventBus;
  40. }
  41.  
  42. @Override
  43. public void post(final Object event) {
  44. this.eventBus.post(event);
  45. }
  46.  
  47. @Override
  48. public void postAsync(final Object event) {
  49. this.asyncEventBus.post(event);
  50. }
  51. }
  52. }

7.EventBusHandler

  1. @Component
  2. @Slf4j
  3. public class EventBusHandler {
  4.  
  5. @Autowired
  6. private final EventBusSupport eventBusSupport;
  7.  
  8. public EventBusHandler(final EventBusSupport eventBusSupport){
  9. this.eventBusSupport = eventBusSupport;
  10. }
  11.  
  12. public void eventPost(){
  13. eventBusSupport.post(MessageEvent.builder().id(1).name("test").build());
  14. log.info("post event");
  15. eventBusSupport.postAsync(MessageEvent.builder().id(2).name("AsyncTest").build());
  16. log.info("post async event");
  17. }
  18. }

8.运行测试类

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class EventbusApplicationTests {
  4.  
  5. @Autowired
  6. private EventBusHandler eventBusHandler;
  7.  
  8. @Test
  9. public void contextLoads() {
  10. eventBusHandler.eventPost();
  11. }
  12. }

结果

  1. 2019-11-03 20:50:02.028 INFO 12292 --- [ main] com.sww.eventbus.listener.EventListener : Subscribe message:MessageEvent(id=1, name=test)
  2. 2019-11-03 20:50:02.028 INFO 12292 --- [ main] c.sww.eventbus.publish.EventBusHandler : post event
  3. 2019-11-03 20:50:02.044 INFO 12292 --- [ main] c.sww.eventbus.publish.EventBusHandler : post async event
  4. 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

本文系本人原创,同步更新在我的独立博客http://791202.com/上,如要转载,请注明出处!

SpringBoot+EventBus使用教程(二)的更多相关文章

  1. SpringBoot+EventBus使用教程(一)

    一.简介 EventBus是一个基于发布订阅的事件总线,在Java和Android里都可以使用. 二.使用 1.引入pom <dependency> <groupId>org. ...

  2. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  3. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  4. Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...

  5. 很详细的SpringBoot整合UEditor教程

    很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529    版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...

  6. CRL快速开发框架系列教程二(基于Lambda表达式查询)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  7. 手把手教从零开始在GitHub上使用Hexo搭建博客教程(二)-Hexo参数设置

    前言 前文手把手教从零开始在GitHub上使用Hexo搭建博客教程(一)-附GitHub注册及配置介绍了github注册.git相关设置以及hexo基本操作. 本文主要介绍一下hexo的常用参数设置. ...

  8. C#微信公众号开发系列教程二(新手接入指南)

    http://www.cnblogs.com/zskbll/p/4093954.html 此系列前面已经更新了两篇博文了,都是微信开发的前期准备工作,现在切入正题,本篇讲解新手接入的步骤与方法,大神可 ...

  9. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

随机推荐

  1. laravel 广播细节讲解

    1.应用场景 1.通知(Notification) 或 信号(Signal) 2.通知是最简单的示例,也最经常用到.信号也可看作是通知的一种展现形式,只不过信号没有UI而已. 3.Activity S ...

  2. LOOP AT GROUP语法练习

    DATA:P_MENGE TYPE EKKO-WKURS. DATA:P_MENGE1 TYPE EKKO-WKURS. SELECT * FROM EKKO INTO TABLE @DATA(LT_ ...

  3. WPF ItemsSource Order by Getter

    public ObservableCollection<CustomerModel> CustomerCollection { get { if(customerCollection!=n ...

  4. WPF-数据模板深入(加载XML类型数据)

    一.我们知道WPF数据模板是当我们给定一个数据类型,我们为这个数据类型写好布局,就给这种数据类型穿上了外衣. 下面这个例子,能够帮助大家充分理解数据模板就是数据类型的外衣的意思:(里面的MyListB ...

  5. ASP.NET Core Web 项目文件

    在本节中,我们将探索并了解 asp.net core 项目文件. 我们使用 C#作为编程语言,因此项目文件具有.csproj 扩展名. 如果您使用过以前版本的 ASP.NET,那么您可能对此文件非常熟 ...

  6. QT4.8.7和VS2010环境搭建及使用

    (一)环境搭建 首先下载QT4.8.7的安装包.QT Addin 1.11插件和VS2010安装包.第一步:安装好VS2010第二步:安装QT4.8.7(qt-opensource-windows-x ...

  7. hadoop 完全分布式集群搭建

    1.在伪分布式基础上搭建,伪分布式搭建参见VM上Hadoop3.1伪分布式模式搭建 2.虚拟机准备,本次集群采用2.8.3版本与3.X版本差别不大,端口号所有差别 192.168.44.10 vmho ...

  8. Linux命令: ps

    STAT 进程状态 S-睡眠 s-进程是会话向导进程 N拥有比普通优先级更低的 R-正在运行 D-短期等待 Z-僵尸进程 T被跟踪或者被停止 STATED 进程启动时间 TIME  进程使用CPU时间 ...

  9. mac上配置python的安装环境杂记

    现在的python的包都是通过pip安装的. 所以非常重要的一步是配置pip的安装源 vi ~/.pip/pip.conf [global] index-url = http://pypi.douba ...

  10. Redis缓存实战教程

    目录 Redis缓存 使用缓存Redis解决首页并发问题 1.缓存使用的简单设计 2.Redis的整合步骤 A 将Redis整合到项目中(Redis+Spring) B 设计一个数据存储策越 3.Re ...