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