An event bus package to use spring project. It is Spring Version of used EventBus package on this Microservice project tutorial
There is only RabbitMQ implementation with basic sub-pub,however you can improve current implementation or create for other services by implementing IEventBus
Create instance using EventBusFactory with given config its by default uses rabbitmq configs :
@Configuration
public class Config {
@Autowired
ApplicationContext ap;
@Bean
public IEventBus getEventBus() throws IOException, TimeoutException {
// Config
EventBusConfig config = new EventBusConfig();
config.setConnectionRetryCount(5);
config.setDefaultTopicName("MyMicroServiceProject");
config.setEventNameSuffix("IntegrationEvent");
config.setSubscriberClientAppName("OrderService");
config.setEventBusType(EventBusType.RabbitMQ);
// Create EventBus
return EventBusFactory.create(config, ap);
}
}
Define prefix and suffix of Events, and need to name Events in conformity with these rules to be able to correctly route messages.
To publish event need to create event class named according to suffix,prefix and need to inherit IntegrationEvent.
public class CityCreatedIntegrationEvent extends IntegrationEvent {
public City city;
}
After that publish it using event-bus:
@Service
public class CityManager implements ICityService {
@Qualifier("cityCrudRepository")
@Autowired
private ICityDal cityDal;
@Autowired
IEventBus eventBus;
@Override
public void add(City city) {
this.cityDal.add(city);
eventBus.publish(new CityCreatedIntegrationEvent(city));
}
}
Subscribers creates queues and routingKey with current configurations defined as given Events class name without defined prefix and suffix.
eventBus.subscribe(CityCreatedIntegrationEvent.class, CityIntegrationEventHandler.class);
This will create Queue as OrderService.CityCreated under MyMicroServiceProject exchange.
To subscribe you need to create an EventHandler class for event you want to sub and need same event class you published before.
public class CityCreatedIntegrationEvent extends IntegrationEvent {
private City city;
}
@Component
public class CityIntegrationEventHandler implements IIntegrationEventHandler<CityCreatedIntegrationEvent> {
@Override
public Future<Void> handle(CityCreatedIntegrationEvent event) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
return (Future<Void>) executorService.submit(() -> System.out.println("CityIntegration event Handled for city: "+event.toString()));
}
}
EventHandler class must inherit IIntegrationEventHandler and register it to application context;
When event arrives to subscriber service's Queue it will be consumed with execution of the provided 'handle()' method in EventHandler.