Skip to content

Instantly share code, notes, and snippets.

@0532
Created January 23, 2016 02:01
Show Gist options
  • Save 0532/7631c6be2ad3a4be10c0 to your computer and use it in GitHub Desktop.
Save 0532/7631c6be2ad3a4be10c0 to your computer and use it in GitHub Desktop.
package io.terminus.neverest.notice.dao.event;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* Created by Wang Lichao at 16/1/21 22:19.
* Desc:
*/
public class EventBusTest {
public static void main(String[] args) {
EventBus eventBus = new EventBus();
/**
* 注册事件处理器
*/
eventBus.register(new Object(){
@Subscribe
public void handleUserInfoChangeEvent(UserInfoChangeEvent userInfoChangeEvent){
System.out.println("处理用户信息变化事件:" + userInfoChangeEvent.getUserName());
}
@Subscribe
public void handleUserInfoChangeEvent(BaseEventBusEvent userInfoChangeEvent){
System.out.println("所有事件的父类");
}
});
eventBus.post(new UserInfoChangeEvent("apple"));
}
/**
* 异步的EventBus
*/
public static void asyncEventBusTest(){
Executor executor = Executors.newFixedThreadPool(10);
AsyncEventBus asyncEventBus = new AsyncEventBus("asyncEventBus", executor);
/**
* 注册事件处理器
*/
asyncEventBus.register(new Object(){
@Subscribe
public void handleUserInfoChangeEvent(UserInfoChangeEvent userInfoChangeEvent){
System.out.println("处理用户信息变化事件:" + userInfoChangeEvent.getUserName());
}
@Subscribe
public void handleUserInfoChangeEvent(BaseEventBusEvent userInfoChangeEvent){
System.out.println("所有事件的父类");
}
});
asyncEventBus.post(new UserInfoChangeEvent("apple"));
System.out.println("异步EventBus");
}
static class BaseEventBusEvent{
}
static class UserInfoChangeEvent extends BaseEventBusEvent{
private String userName;
public UserInfoChangeEvent(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment