简介

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

使用

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所在的包变了。

3.MessageEvent

和上篇一样。

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。

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

结果

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

本文系本人原创,同步更新在我的独立博客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. LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

    题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...

  2. C#中的一些对话框问题处理

    1. 对于打开文件对话框处理 #region 打开文件对话框 string StrPath; OpenFileDialog Flag = new OpenFileDialog(); Flag.Mult ...

  3. Winform中设置ZedGraph的X轴与Y轴的刻度不在对面显示

    场景 C#窗体应用中使用ZedGraph曲线插件绘制图表: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99716066 Win ...

  4. 操作系统原理之I/O设备管理(第六章上半部分)

    一.I/O系统的组成 I/O系统不仅包括各种I/O设备,还包括与设备相连的设备控制器,有些系统还配备了专⻔⽤ 于输⼊/输出控制的专⽤计算机,即通道.此外,I/O系统要通过总线与CPU.内存相连. I/ ...

  5. delphi FillChar的用法(转)

    delphi FillChar的用法(转) (2012-12-24 15:12:06) 转载▼ 标签: it 分类: delphi7 FillChar的用法(delphi) Fillchar是Turb ...

  6. JavaScript调用百度地图

    在网站开发过程中,经常会调用到地图,百度地图提供Web开发.Android开发.iOS开发API及SDK,百度地图JavaScript API可帮助您在网站中构建功能丰富.交互性强的地图应用,本篇博客 ...

  7. 记录几个 Android x86 系统的官网

    首先是官网:https://www.android-x86.org/ 国内: 凤凰OS(Android 7.1):http://www.phoenixos.com/download_x86 技德Rem ...

  8. vue学习指南:第二篇(详细Vue基础) - Vue的指令

    一. Vue 的介绍 1. vue是一个 mvvm 的框架.(面试官经常会问的),angular 是 mvc的框架. 2. vm 是 vum 的实例,这个实例存在计算机内存中,主要干两件大事: 1. ...

  9. PowerShell美化

    转载自Powershell 美化 --oh-my-posh,作者Zvonimir. PowerShell默认的主题太丑了,用过OhMyZsh之后是无法忍受这种丑陋的,幸好PowerShell有对应的O ...

  10. python 之serial、pyusb 使用开发

    说明:本次是在windows 系统操作实现的. serial 使用场景,获取得力扫码枪的扫码数据,该扫码枪支持三种通讯接口设置,如下图 即插即用的是 USB-KBW功能,插上去即可获取扫码数据,第二种 ...