Guava入门~EventBus~细粒度订阅

将交易细分为买/卖:

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));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值