Guava(이벤트 버스): 이벤트 버스 이벤트버스
5329 단어 이벤트 버스:이벤트 버스 이벤트 버스
EventBus 인스턴스를 만들려면 다음과 같이 하십시오.
EventBus eventBus = new EventBus();
//
EventBus eventBus = new EventBus(TradeAccountEvent.class.getName());// ,
이벤트에 가입하려면 다음과 같이 하십시오.
/**
*
*/
public class TradeAccountEvent {
private double amount;
private Date tradeExecutionTime;
private TradeType tradeType;
private TradeAccount tradeAccount;
public TradeAccountEvent(TradeAccount account, double amount,
Date tradeExecutionTime, TradeType tradeType) {
this.amount = amount;
this.tradeExecutionTime = tradeExecutionTime;
this.tradeAccount = account;
this.tradeType = tradeType;
}
}
/**
*
*/
public class BuyEvent extends TradeAccountEvent {
public BuyEvent(TradeAccount tradeAccount, double amount,
Date tradExecutionTime) {
super(tradeAccount, amount, tradExecutionTime, TradeType.BUY);
}
}
/**
*
*/
public class SellEvent extends TradeAccountEvent {
public SellEvent(TradeAccount tradeAccount, double amount,
Date tradExecutionTime) {
super(tradeAccount, amount, tradExecutionTime, TradeType.SELL);
}
}
/**
* ,
*/
public class AllTradesAuditor {
private List<BuyEvent> buyEvents = Lists.newArrayList();
private List<SellEvent> sellEvents = Lists.newArrayList();
public AllTradesAuditor(EventBus eventBus) {
eventBus.register(this);
}
/**
*
*/
@Subscribe
public void auditSell(SellEvent sellEvent) {
sellEvents.add(sellEvent);
System.out.println("Received TradeSellEvent " + sellEvent);
}
/**
*
*/
@Subscribe
public void auditBuy(BuyEvent buyEvent) {
buyEvents.add(buyEvent);
System.out.println("Received TradeBuyEvent " + buyEvent);
}
}
/**
* ,
*/
public class SimpleTradeExecutor {
private EventBus eventBus;
public SimpleTradeExecutor(EventBus eventBus) {
this.eventBus = eventBus;
}
/**
*
*/
public void executeTrade(TradeAccount tradeAccount, double amount,
TradeType tradeType) {
TradeAccountEvent tradeAccountEvent = processTrade(tradeAccount,
amount, tradeType);
eventBus.post(tradeAccountEvent); //
}
/**
*
*
* @return
*/
private TradeAccountEvent processTrade(TradeAccount tradeAccount,
double amount, TradeType tradeType) {
Date executionTime = new Date();
String message = String.format(
"Processed trade for %s of amount %n type %s @ %s",
tradeAccount, amount, tradeType, executionTime);
TradeAccountEvent tradeAccountEvent;
if (tradeType.equals(TradeType.BUY)) { //
tradeAccountEvent = new BuyEvent(tradeAccount, amount,
executionTime);
} else { //
tradeAccountEvent = new SellEvent(tradeAccount, amount,
executionTime);
}
System.out.println(message);
return tradeAccountEvent;
}
}
EventBus eventBus = new EventBus();
AllTradesAuditor auditor = new AllTradesAuditor(eventBus);
SimpleTradeExecutor tradeExecutor = new SimpleTradeExecutor(eventBus);
tradeExecutor.executeTrade(new TradeAccount(), 1000, TradeType.SELL);
tradeExecutor.executeTrade(new TradeAccount(), 2000, TradeType.BUY);
구독 해지:
public void unregister(){
this.eventBus.unregister(this);
}
AsyncEventBus 클래스
AsyncEventBus asyncEventBus = new AsyncEventBus(executorService);
DeadEvents:
public class DeadEventSubscriber {
private static final Logger logger =
Logger.getLogger(DeadEventSubscriber.class.getName());
public DeadEventSubscriber(EventBus eventBus) {
eventBus.register(this);
}
/**
*
*/
@Subscribe
public void handleUnsubscribedEvent(DeadEvent event){
logger.warning("No subscribers for "+event.getEvent());
}
}
의존 주입
@Component
public class SimpleTradeExecutor {
private EventBus eventBus;
@Autowired
public SimpleTradeExecutor(EventBus eventBus) {
this.eventBus = checkNotNull(eventBus, "EventBus can't be null"); }
}
@Component
public class SimpleTradeAuditor {
private List<TradeAccountEvent> tradeEvents =
Lists.newArrayList();
@Autowired
public SimpleTradeAuditor(EventBus eventBus){
checkNotNull(eventBus,"EventBus can't be null");
eventBus.register(this);
}
}
지금까지 Guava의 Event Bus에 대해 설명했습니다.