Guava EventBus集成spring
EventBus 不是通用的消息系统,也不是用来做进程间的通信的,而是在进程内,用于解耦两段直接调用的业务逻辑;
1、代码结构

- event:eventbus中流转的事件(消息),包结构按照业务模块在细分(比如应用部署模块就是deployment);
- subscriber:消费者,和event 是一一对应的,一个event 对应一个消费者,包结构按照业务模块在细分(比如应用部署模块就是deployment);
- poster:生产者,这边把生产者单独出来是为了收敛入口,这样可以方便的知道有哪些地方在生产消息,按照业务模块分为不同的类(因为生产消息的功能比较单薄);
2、代码实现
在applicationContext.xml 中定义好EventBus
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" lazy-init="true"> <property name="corePoolSize" value="10"/> <property name="maxPoolSize" value="50"/> <property name="queueCapacity" value="10000"/> <property name="keepAliveSeconds" value="300"/> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/> </property></bean><bean id="asyncEventBus" class="com.google.common.eventbus.AsyncEventBus"> <constructor-arg name="executor" ref="taskExecutor"/></bean> |
2.1、标准化subscriber
所有的subscriber都要实现 BaseSubscriber这个 interface
public interface BaseSubscriber<E> { /** * event 处理逻辑入口 **/ void subscribe(E event);} |
所有的subscriber在类上加上EventBusRegister 这个annotation
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface EventBusRegister {} |
实现EventBusAdapter用于自动注册subscriber
@Componentpublic class EventBusAdapter implements ApplicationContextAware, InitializingBean { @Autowired private AsyncEventBus asyncEventBus; private ApplicationContext applicationContext; @Override public void afterPropertiesSet() throws Exception { this.applicationContext.getBeansWithAnnotation(EventBusRegister.class).forEach((name, bean) -> { asyncEventBus.register(bean); }); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }} |
举个例子
@Component@EventBusRegisterpublic class BuildUpdateSubscriber implements BaseSubscriber<BuildUpdateEvent> { @Autowired private BuildService buildService; @Subscribe @Override public void subscribe(BuildUpdateEvent event) { switch (event.getEventType()) { case BUILD_CONNECTED: List<BuildVo> buildVos = (List<BuildVo>) event.getData(); buildService.addBuildVosAndTriggerConnectEvent(buildVos); break; case BUILD_ADD: BuildVo addedBuildVo = (BuildVo) event.getData(); buildService.addBuildVoAndTriggerClientEvent(addedBuildVo); break; case BUILD_MODIFY: BuildVo modifiedBuildVo = (BuildVo) event.getData(); buildService.modifyBuildVoAndTriggerEvent(modifiedBuildVo); break; case BUILD_DELETE: BuildVo deletedBuildVo = (BuildVo) event.getData(); buildService.deleteBuildVoAndTriggerClientEvent(deletedBuildVo); break; default: // ignore break; } }} |
3、代码实现改进
前面通过规范代码的包结构、加了一些trick使得我们可以方便的使用eventbus解耦我们的业务逻辑,但是有时候我们需要的bean被注册 的前后做一些业务逻辑,所以我们在bean 被注册到eventbus前后加了两个hook:AfterRegisterProcessor、BeforeRegisterProcessor;实现这两个interface并且实现对于的方法,会在bean 被注册前后被调用
bean 注册到eventbus前的hook
public interface BeforeRegisterProcessor { void beforeRegister();} |
bean 注册到eventbus后的hook
public interface AfterRegisterProcessor { void afterRegister();} |
实现:保证在 client.watch 之前,注册已经完成,这样watch产生的消息就能够保证被成功消费
@Servicepublic class GlueService implements AfterRegisterProcessor { @Autowired private PodListener podListener; @Autowired private RouteListener routerListener; @Autowired private BuildListener buildListener; @Autowired private DeploymentListener deploymentListener; @Autowired private OpenShiftClient openShiftClient; @Override public void afterRegister() { IClient client = openShiftClient.getClient(); podWatch = client.watch(podListener, ResourceKind.POD); routeWatch = client.watch(routerListener, ResourceKind.ROUTE); buildWatch = client.watch(buildListener, ResourceKind.BUILD); deploymentWatch = client.watch(deploymentListener, ResourceKind.REPLICATION_CONTROLLER); }} |
Guava EventBus集成spring的更多相关文章
- guava cache与spring集成
缓存的背景 缓存,在我们日常开发中是必不可少的一种解决性能问题的方法.简单的说,cache 就是为了提升系统性能而开辟的一块内存空间.在cpu进行计算的时候, 首先是读取寄存器,然后内存,再是硬盘.由 ...
- EventBus VS Spring Event
EventBus VS Spring Event 本地异步处理,采用事件机制 可以使 代码解耦,更易读.事件机制实现模式是 观察者模式(或发布订阅模式),主要分为三部分:发布者.监听者.事件. Gua ...
- MyBatis6:MyBatis集成Spring事物管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...
- Guava - EventBus(事件总线)
Guava在guava-libraries中为我们提供了事件总线EventBus库,它是事件发布订阅模式的实现,让我们能在领域驱动设计(DDD)中以事件的弱引用本质对我们的模块和领域边界很好的解耦设计 ...
- CXF集成Spring实现webservice的发布与请求
CXF集成Spring实现webservice的发布(服务端) 目录结构: 主要代码: package com.cxf.spring.pojo; public class User { int id ...
- Dubbo集成Spring与Zookeeper实例
>>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...
- Thymeleaf 集成spring
Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...
- 06_在web项目中集成Spring
在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...
- Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...
随机推荐
- 栈Stack --- 数组实现
栈最大的一个特点就是先进后出(FILO—First-In/Last-Out). /** * 栈:后进先出 * Created by fred on 2018/7/31. */ public class ...
- C语言中#和##的作用
使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. #include <stdio.h> #define STR1(x) #x//使x成为字符串 #define STR2(x, ...
- 转-C++之string判断字符串是否包含某个子串
转自:https://blog.csdn.net/zhouxinxin0202/article/details/77862615/ 1.string类函数find C++的string类提供了字符串中 ...
- Mamen所需要的jar包怎么生成
Mamen所需要的jar包怎么生成 使用 mamen 难免碰到,不知道的 jar 包,不知道怎么在 pom 文件中写,分享一个网址,可以把你想要的 jar 包生成 pom 配置文件,个人感觉非常好用. ...
- 树状数据删除(TP5)
应用场景:类似上图中树状菜单,选中一级菜单 点击上方删除按钮 所有子菜单删除 以下是代码截图(代码基于 TP5)
- MySQL总结02
sql优化 Insert使用批量 查询不要使用*, MySQ需要先查出表里的所有字段,再进行匹配 字符串的查询条件要带引号,否则可能不走索引 备份及恢复 备份 mysqldump -uroot -pp ...
- [轉]Linux 2.6内核笔记【内存管理】
4月14日 很多硬件的功能,物尽其用却未必好过软实现,Linux出于可移植性及其它原因,常常选择不去过分使用硬件特性. 比如 Linux只使用四个segment,分别是__USER_CS.__USER ...
- C#关键字扫盲——Tuple(元组类) 、ValueTuple(值元组)
原文:C#关键字扫盲--Tuple(元组类) .ValueTuple(值元组) 版权声明:本文为博主原创文章,随意转载. https://blog.csdn.net/Michel4Liu/articl ...
- Android多线程:深入分析 Handler机制源码(二)
前言 在Android开发的多线程应用场景中,Handler机制十分常用 接下来,深入分析 Handler机制的源码,希望加深理解 目录 1. Handler 机制简介 定义一套 Android 消息 ...
- Msf+Donut执行任意可执行文件
地址:donut:https://github.com/TheWover/donut 介绍:是一个shellcode生成工具,可以将.NET程序集等转换为shellcode. 使用:donut.exe ...