在工作中,经常会遇见使用异步的方式来发送事件,或者触发另外一个动作:经常用到的框架是MQ(分布式方式通知)。如果是同一个jvm里面通知的话,就可以使用guava的EventBus事件机制。
EventBus事件机制包括三个部分:事件、事件监听器、事件源。
一、引入guava的jar以及反射的jar包
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
二、在config下新建一个类EventBusConfig.java
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.Subscribe;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.Executors;
@Component
public class EventBusConfig {
@Autowired
private ApplicationContext context;
@Bean
@ConditionalOnMissingBean(AsyncEventBus.class)
AsyncEventBus createEventBus() {
AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(5));
Reflections reflections = new Reflections("com.xxx", new MethodAnnotationsScanner());
Set<Method> methods = reflections.getMethodsAnnotatedWith(Subscribe.class);
if (null != methods ) {
for(Method method : methods) {
try {
eventBus.register(context.getBean(method.getDeclaringClass()));
}catch (Exception e ) {
//register subscribe class error
}
}
}
return eventBus;
}
}
三、利用接口封装事件发送
1、定义接口LocalEventBus.java
public interface LocalEventBus {
void post(Event event);
}
2、定义实现类LocalEventBusImpl.java
import com.google.common.eventbus.AsyncEventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LocalEventBusImpl implements LocalEventBus {
@Autowired
private AsyncEventBus eventBus;
@Override
public void post(Event event) {
if (null != event) {
eventBus.post(event);
}
}
}
3、接口Event.class
public interface Event<T> {
T getContent();
}
四、在业务工程里使用:
需要定义事件、消息体、订阅者、发送者。
1、定义login事件
public class LoginEvent implements Event<LoginMsg> {
private LoginMsg loginMsg;
public LoginEvent(LoginMsg loginMsg) {
this.loginMsg = loginMsg;
}
@Override
public LoginMsg getContent() {
return this.loginMsg;
}
}
2、定义消息体
public class LoginMsg {
private Long uid;
private String mobile;
private String ip;
private String osVersion;
private String deviceModel;
private String deviceToken;
}
3、定义订阅者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class LoginSubscriber {
@Subscribe
@AllowConcurrentEvents
public void onLogin(LoginEvent event) throws BizException {
LoginMsg msg = event.getContent();
Long uid = msg.getUid();
// 具体业务
Thread.sleep(5000);
}
}
4、定义发送者,把消息发送到EventBus。
@Autowired
private LocalEventBus localEventBus;
LoginMsg msg = new LoginMsg(uid, mobile, ip, osVersion, deviceModel, deviceToken);
localEventBus.post(new LoginEvent(msg));
5、controller 测试。
@Autowired
private LocalEventBus localEventBus;
@GetMapping(value = "/sendMsg/{mobile}")
public Result sendMsg(@PathVariable("mobile") String mobile) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
LoginMsg msg = new LoginMsg(1, mobile, ip, 10, 10, 10);
localEventBus.post(new LoginEvent(msg));
stopWatch.stop();
return Result.success(stopWatch.getTime());
}
结果显示,因为咱们在LoginSubscriber中设置了Thread.sleep(5000),所以最后打印的结果可以看出asyncEventBus是异步的。如果使用的是 eventBus.post(……)那就是同步的了。