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 ...
随机推荐
- 【消息中间件】kafka
一.kafka整体架构 kafka是一个发布订阅模式的消息队列,生产者和消费者是多对多的关系,将发送者与接收者真正解耦: 生产者将消息发送到broker: 消费者采用拉(pull)模式订阅并消费消息: ...
- MVC路由解析---UrlRoutingModule
文章引导 MVC路由解析---IgnoreRoute MVC路由解析---MapRoute MVC路由解析---UrlRoutingModule Area的使用 引言: 此文全文内容90%转自 一.前 ...
- C++——class类和struct结构体的唯一区别
唯一的区别:默认访问权限不同,其他没有任何区别. class Base{/****/}; class D1:Base{/****/};//默认D1对Base是public继承 struct D2:Ba ...
- JS 多个条件判断
// 多个条件判断 // 对象序列(Object) 推荐使用这一种 var obj = {'CJ':'成交', 'WCJ':'未成交'}; if (key in obj) { // TODO } // ...
- 201⑨湘潭邀请赛 Chika and Friendly Pairs(HDU6534)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=6534 题意: 给你一个数组,对于第i个数来说,如果存在一个位置j,使得j>i并且a[j]-k&l ...
- 力扣算法——134GasStation【M】
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- java 并发——synchronized
java 并发--synchronized 介绍 在平常我们开发的过程中可能会遇到线程安全性的问题,为了保证线程之间操作数据的正确性,我们第一想到的可能就是使用 synchronized 并且 syn ...
- 神奇,教你用随机数打印hello world
下面是一段随机数程序. public static void main(String[] args) { System.out.println(randomString(-229985452) + & ...
- redis demo
方法hset(String key,String field,String value),hmset(String key, Map<String,String> hash),hgetAl ...
- CF1205B
CF1205B 由鸽巢原理n比较大的时候直接输出3 然后剩下的就可以跑最小环 #include<iostream> #include<cstdio> #include<c ...