Guava入门~EventBus~依赖注入

package bbejeck.guava.chapter7.config;

import com.google.common.eventbus.EventBus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * User: Bill Bejeck
 * Date: 4/26/13
 * Time: 3:48 PM
 */
@Configuration
@ComponentScan(basePackages = {"bbejeck.guava.chapter7.publisher.simple",
                              "bbejeck.guava.chapter7.subscriber.simple"})
public class SimpleEventBusConfig {
    @Bean
    public EventBus eventBus() {
        return new EventBus();
    }
}

注入

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

多EventBus

package bbejeck.guava.chapter7.config;

import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.*;

/**
 * User: Bill Bejeck
 * Date: 4/26/13
 * Time: 3:48 PM
 */
@Configuration
@ComponentScan(basePackages = {"bbejeck.guava.chapter7.publisher.complex",
                              "bbejeck.guava.chapter7.subscriber.complex"})
public class MultipleEventBusConfig {

    @Bean(autowire = Autowire.BY_NAME,name = "salesEventBus")
    public EventBus salesEventBus() {
        return new EventBus();
    }
    @Bean(autowire = Autowire.BY_NAME,name = "buysEventBus")
    public EventBus buysEventBus() {
        return new EventBus();
    }


}
package bbejeck.guava.chapter7.subscriber.complex;

import bbejeck.guava.chapter7.events.BuyEvent;
import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import static com.google.common.base.Preconditions.*;
/**
 * User: Bill Bejeck
 * Date: 4/26/13
 * Time: 12:40 PM
 */
@Component
public class TradeBuyAuditor {

    private List<BuyEvent> buyEvents = Lists.newArrayList();

    @Autowired
    public TradeBuyAuditor(@Qualifier("buysEventBus")EventBus eventBus) {
         checkArgument(eventBus != null,"EventBus can't be null");
         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.subscriber.complex;

import bbejeck.guava.chapter7.events.SellEvent;
import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import static com.google.common.base.Preconditions.*;
/**
 * User: Bill Bejeck
 * Date: 4/26/13
 * Time: 12:40 PM
 */
@Component
public class TradeSellAuditor {

    private List<SellEvent> sellEvents = Lists.newArrayList();

    @Autowired
    public TradeSellAuditor(@Qualifier("salesEventBus") EventBus eventBus) {
        checkArgument(eventBus != null,"EventBus can't be null");
         eventBus.register(this);
    }

    @Subscribe
    public void auditSell(SellEvent sellEvent){
        sellEvents.add(sellEvent);
        System.out.println("Received TradeSellEvent "+sellEvent);
    }

    public List<SellEvent> getSellEvents() {
        return sellEvents;
    }
}

发布者

package bbejeck.guava.chapter7.publisher.complex;

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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * User: Bill Bejeck
 * Date: 4/26/13
 * Time: 11:29 AM
 */
@Component
public class BuyTradeExecutor {

    private EventBus eventBus;

    @Autowired
    public BuyTradeExecutor(@Qualifier("buysEventBus")EventBus eventBus) {
        this.eventBus = eventBus;
    }

    public void executeBuyTrade(TradeAccount tradeAccount, double amount) {
        TradeAccountEvent tradeAccountEvent = processTrade(tradeAccount, amount, TradeType.BUY);
        eventBus.post(tradeAccountEvent);
    }

    private TradeAccountEvent processTrade(TradeAccount tradeAccount, double amount, TradeType tradeType) {
        Date executionTime = new Date();
        String message = String.format("Processed buy for %s of amount %n  @ %s", tradeAccount, amount, tradeType, executionTime);
        TradeAccountEvent tradeAccountEvent;
        tradeAccountEvent = new BuyEvent(tradeAccount, amount, executionTime);
        System.out.println(message);
        return tradeAccountEvent;
    }
}
package bbejeck.guava.chapter7.publisher.complex;

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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * User: Bill Bejeck
 * Date: 4/26/13
 * Time: 11:29 AM
 */
@Component
public class SellTradeExecutor {

    private EventBus eventBus;

    @Autowired
    public SellTradeExecutor(@Qualifier("salesEventBus")EventBus eventBus) {
        this.eventBus = eventBus;
    }

    public void executeSaleTrade(TradeAccount tradeAccount, double amount) {
        TradeAccountEvent tradeAccountEvent = processTrade(tradeAccount, amount, TradeType.SELL);
        eventBus.post(tradeAccountEvent);
    }

    private TradeAccountEvent processTrade(TradeAccount tradeAccount, double amount, TradeType tradeType) {
        Date executionTime = new Date();
        String message = String.format("Processed sale for %s of amount %n  @ %s", tradeAccount, amount, tradeType, executionTime);
        TradeAccountEvent tradeAccountEvent;
        tradeAccountEvent = new SellEvent(tradeAccount, amount, executionTime);
        System.out.println(message);
        return tradeAccountEvent;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值