将交易细分为买/卖:
public class SellEvent extends TradeAccountEvent {
public SellEvent(TradeAccount tradeAccount, double amount, Date tradExecutionTime) {
super(tradeAccount, amount, tradExecutionTime, TradeType.SELL);
}
}
public class BuyEvent extends TradeAccountEvent {
public BuyEvent(TradeAccount tradeAccount, double amount, Date tradExecutionTime) {
super(tradeAccount, amount, tradExecutionTime, TradeType.BUY);
}
}
处理类也区分
public class TradeSellAuditor {
private List<SellEvent> sellEvents = Lists.newArrayList();
public TradeSellAuditor(EventBus eventBus) {
eventBus.register(this);
}
@Subscribe
public void auditSell(SellEvent sellEvent){
sellEvents.add(sellEvent);
System.out.println("Received SellEvent "+sellEvent);
}
public List<SellEvent> getSellEvents() {
return sellEvents;
}
}
public class TradeBuyAuditor {
private List<BuyEvent> buyEvents = Lists.newArrayList();
public TradeBuyAuditor(EventBus eventBus) {
eventBus.register(this);
}
@Subscribe
public void auditBuy(BuyEvent buyEvent){
buyEvents.add(buyEvent);
System.out.println("Received TradeBuyEvent "+buyEvent);
}
public List<BuyEvent> getBuyEvents() {
return buyEvents;
}
}
发送事件区分类型
package bbejeck.guava.chapter7.publisher;
import bbejeck.guava.chapter7.events.BuyEvent;
import bbejeck.guava.chapter7.events.SellEvent;
import bbejeck.guava.chapter7.events.TradeAccountEvent;
import bbejeck.guava.chapter7.events.TradeType;
import bbejeck.guava.common.model.TradeAccount;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* User: Bill Bejeck
* Date: 4/26/13
* Time: 11:29 AM
*/
public class BuySellTradeExecutor {
private EventBus eventBus;
public BuySellTradeExecutor(EventBus eventBus) {
this.eventBus = eventBus;
}
public void executeTrade(TradeAccount tradeAccount, double amount, TradeType tradeType) {
TradeAccountEvent tradeAccountEvent = processTrade(tradeAccount, amount, tradeType);
eventBus.post(tradeAccountEvent);
}
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;
}
}
测试示例
package bbejeck.guava.chapter7.subscriber;
import bbejeck.guava.chapter7.EventBusTestBase;
import bbejeck.guava.chapter7.subscriber.complex.TradeBuyAuditor;
import bbejeck.guava.chapter7.subscriber.complex.TradeSellAuditor;
import com.google.common.eventbus.EventBus;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
* User: Bill Bejeck
* Date: 4/28/13
* Time: 8:49 PM
*/
public class TradeBuySellAuditorTest extends EventBusTestBase {
private TradeBuyAuditor buyAuditor;
private TradeSellAuditor sellAuditor;
private EventBus eventBus;
@Before
public void setUp(){
eventBus = getEventBus();
buyAuditor = new TradeBuyAuditor(eventBus);
sellAuditor = new TradeSellAuditor(eventBus);
}
@Test
public void sellOnlyTest(){
eventBus.post(sellEventBuilder().build());
assertThat(sellAuditor.getSellEvents().size(),is(1));
assertThat(buyAuditor.getBuyEvents().isEmpty(),is(true));
}
@Test
public void buyOnlyTest(){
eventBus.post(buyEventBuilder().build());
assertThat(sellAuditor.getSellEvents().isEmpty(),is(true));
assertThat(buyAuditor.getBuyEvents().size(),is(1));
}
}