Created
January 23, 2016 02:01
-
-
Save 0532/7631c6be2ad3a4be10c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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