The easiest way to explain it is to show an example, so, a simple service that publish an event as part of it's business logic:
And for the other end, the subscriber:
@Service
public class GroundControlService {
@Autowired
@Qualifier("eventBus")
private EventBus eventBus;
@Override
public void receive(Event event) {
if (event instanceof BigLandingSuccess) {
eventBus.publish("earthChannel", new Celebration("We did it!"));
this.bringOutTheChampagne(ALL);
eventBus.publish("supplierChannel", new MissingResource("Champagne"));
}
}
}
@Service
public class SupplierService {
@Autowired
@Qualifier("eventBus")
private EventBus eventBus;
public void init() {
eventBus.subscribe("supplierChannel", new EventSubscriber() {
@Override
public void receive(Event event) {
if (event instanceof MissingResource) {
raceForContract(event);
}
}
});
}
}
Not so hard.
Now, here and there I've used the term 'event bus', and you have also seen it in java code above. Next time we will look closer into the bus. Why is there an abstraction and what can you do with it?
No comments:
Post a Comment