evevntbus 负责register和post event.
public class TradeAuditor {
@Subscribe
@AllowConcurrentEvents
public void auditTrade(TradeAccountEvent tradeAccountEvent) {
System.out.println("thread" + Thread.currentThread().getName());
System.out.println("recive event");
}
public TradeAuditor(EventBus eventBus) {
eventBus.register(this);
}
}
在register中,会解析subscribe注解对应的函数为event的handler.@AllowConcurrentEvents表明这个handler是同步的。
public class TradeExcutor {
private EventBus eventBus;
public TradeExcutor(EventBus eventBus) {
this.eventBus = eventBus;
}
public void executeTrade(double amount) {
TradeAccountEvent tradeAccountEvent = new TradeAccountEvent(amount, new Date());
System.out.println("post event");
eventBus.post(tradeAccountEvent);
}
}
在post event,会找到这个event对应的handler,调用,这里没有异步调用,并且会让上一次的handler执行完成后才执行下一个handler.
原文:http://www.cnblogs.com/jsy306/p/4968533.html