This EventBus is based on Guava. Hopefully it doesn't break the license (otherwise please contact me).
Provided modifications opens this EventBus for magic things like:
- changing event handler annotation
- providing strategy for event dispatching -
DispatchStrategy
- providing strategy for finding handler methods -
MethodHandlerFinder
- providing exception handler -
ExceptionHandler
- using parameters of primitive types in handler methods
- ...and it doesn't depend on guava library (Guava is a sizeable jar file)
Example configuration of EventBus
EventBus defaultBus = new EventBus(); // Uses default settings: exception logging, dead event logging, searches for methods annotated with @EventHandler
EventBus simpleBus = EventBus.builder()
.withLoggingExceptionHandler() // Logs exceptions
.withDeadEventLogHandler() // Logs dead events
.withAsyncDispatchStrategy() // Dispatches events in asynchronous way (you can change it to SyncDispatchStrategy)
.annotatedMethodHandlerFindingStrategy(YourAnnotation.class) // Searches for methods annotated with @YourAnnotation
.buildEventBus();
EventBus advancedBus = EventBus.builder()
.exceptionHandler(yourExceptionHandler)
.eventDispatchStrategy(yourEventDispatchStrategy)
.methodHandlerFindingStrategy(yourMethodHandlerFindingStrategy)
.buildEventBus();
Along with flexibus there is also flexibus-spring project that integrates eventbus with spring environment. Try it out or take a look at examples:
Configuration
@Configuration
@ComponentScan(basePackageClasses = TestConfiguration.class)
public class TestConfiguration {
@Bean
public EventBus eventBus() {
return new EventBus();
}
@Bean
public SpringEventBusPostProcessor springEventBusPostProcessor() {
return new SpringEventBusPostProcessor(eventBus());
}
}
Event emitter
@Component
public class EventEmitter {
@Autowired
private EventBus eventBus;
@Test
public void shouldDeliverEvent() {
eventBus.post("Hello");
}
}
Event receiver
@Component
public class EventReceiver {
@EventHandler
public void handle(String event) {
// ...
}
}
In order to use this library add repository location into your pom.xml
and add appropriate dependency.
<dependency>
<groupId>net.exacode.eventbus</groupId>
<artifactId>flexibus</artifactId>
<version>${project.version}</version>
</dependency>