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 ...
随机推荐
- switch 使用使用小技巧
for (int i=0;i<100;i++) { switch (i) { case 1 ... 10: NSLog(@"case 1 ... 10: = %d",i); ...
- 使用Canvas操作像素
现代浏览器支持通过<video>元素播放视频.大多数浏览器也可以通过MediaDevices.getUserMedia() API访问摄像头.但即使这两件事结合起来,我们也无法直接访问和操 ...
- python2读取EXCEL表格内的数据时碰到的问题
一,今天在剥离自动化的测试数据时,发生了一个错误,错误显示读取不到某个单元格的数据. 因为我使用的是python2,正好那一个单元格出现的是中文汉字,再者通过查看报错日志,让我了解到错误的原因. di ...
- 用 Flask 来写个轻博客 (16) — MV(C)_Flask Blueprint 蓝图
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 Blueprint 蓝图 定义一个蓝图 注册一个蓝图 创建蓝 ...
- 转 loadrunner11 录制 chrome 浏览器
chrome不设置代理的原始状态 图1 [LoadRunner]解决LR11无法录制Chrome浏览器脚本问题 LoadRunner录制脚本时,遇到高版本的IE.FireFox,或者Chrome浏 ...
- 转 MySQL数据库面试问题集锦
如何设计一个高并发的系统 ① 数据库的优化,包括合理的事务隔离级别.SQL语句优化.索引的优化 ② 使用缓存,尽量减少数据库 IO ③ 分布式数据库.分布式缓存 ④ 服务器的负载均衡 锁的优化策略 ① ...
- spark性能调优05-troubleshooting处理
1.调节reduce端缓冲区大小避免OOM异常 1.1 为什么要调节reduce端缓冲区大小 对于map端不断产生的数据,reduce端会不断拉取一部分数据放入到缓冲区,进行聚合处理: 当map端数据 ...
- Inversion of Control 控制反转 有什么好处
作者:Mingqi链接:https://www.zhihu.com/question/23277575/answer/169698662来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- sql语句练习50题(Mysql版-详加注释)
表名和字段 1.学生表 Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表 Course(c_id, ...
- svnlook - Subversion 仓库检索工具
SYNOPSIS 总览 svnlook command /path/to/repos [options] [args] OVERVIEW 概述 Subversion 是一个版本控制系统,允许保存旧版本 ...