Skip to content

Instantly share code, notes, and snippets.

@tadas-subonis
Created March 30, 2013 22:55
Show Gist options
  • Save tadas-subonis/5278708 to your computer and use it in GitHub Desktop.
Save tadas-subonis/5278708 to your computer and use it in GitHub Desktop.
class OrderPlacedEvent {
private Order order;
}
class ShopSellerService {
@Inject
private EventBus eventBus;
public void sell(Order order) {
order.toPlaced();
eventBus.publish(new OrderPlacedEvent(order));
}
}
class SmsNotifier {
@Inject
private EventBus eventBus;
@Inject
private SmsGateway smsGateway;
@Listener
public void listen(OrderPlacedEvent event) {
Template message = TemplateFactory.createOrderPlacedMessage();
/*
* Infer destination customer and other variables for template
*/
Message message = message.assign(event.getOrder());
smsGateway.send(message);
}
@PostConstruct
public void loaded() {
/*
* After the bean has been initialized we start listening for events on
* event bus. There is no need to unsubscribe on PreDestroy because we
* assume this will get called when the whole application is stopped -
* this listener is application scoped.
*/
eventBus.subscribe(this);
}
}
class WarehouseJobUpdator {
@Inject
private EventBus eventBus;
@Inject
private JmsGateway jmsGateway;
@Listener
public void listen(OrderPlacedEvent event) {
/*
* Forward message to Warehouse JMS queue
*/
jmsGateway.sendToWarehouse(event);
}
@PostConstruct
public void loaded() {
eventBus.subscribe(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment