EventBus 不是通用的消息系统,也不是用来作进程间的通讯的,而是在进程内,用于解耦两段直接调用的业务逻辑;java
在applicationContext.xml 中定义好EventBusspring
<
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
>
|
全部的subscriber都要实现 BaseSubscriber这个 interfaceapi
public
interface
BaseSubscriber<E> {
/**
* event 处理逻辑入口
**/
void
subscribe(E event);
}
|
全部的subscriber在类上加上EventBusRegister 这个annotationapp
@Target
({ElementType.TYPE})
@Retention
(RetentionPolicy.RUNTIME)
@Documented
public
@interface
EventBusRegister {
}
|
实现EventBusAdapter用于自动注册subscriberasync
@Component
public
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;
}
}
|
举个例子ide
@Component
@EventBusRegister
public
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
;
}
}
}
|
前面经过规范代码的包结构、加了一些trick使得咱们能够方便的使用eventbus解耦咱们的业务逻辑,可是有时候咱们须要的bean被注册 的先后作一些业务逻辑,因此咱们在bean 被注册到eventbus先后加了两个hook:AfterRegisterProcessor、BeforeRegisterProcessor;实现这两个interface而且实现对于的方法,会在bean 被注册先后被调用post
bean 注册到eventbus前的hookui
public
interface
BeforeRegisterProcessor {
void
beforeRegister();
}
|
bean 注册到eventbus后的hookthis
public
interface
AfterRegisterProcessor {
void
afterRegister();
}
|
实现:保证在 client.watch 以前,注册已经完成,这样watch产生的消息就可以保证被成功消费google
@Service
public
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);
}
}
|