This project provides Spring schema-based extension for scanning and registering Guava EventBus event handlers
Clone from GIT and then use Gradle:
$ git clone ...
$ gradle build (or './gradlew build' if you don't have gradle in your path)
Here is a simple example:
public interface EventHandler { void handle(T event); }
implementation:
public class AlertEventHandler implements EventHandler<String> {
private static final Logger LOGGER = LoggerFactory.getLogger(AlertEventHandler.class);
@Override
@Subscribe
public void handle(String event) {
LOGGER.info("GOT EVENT: {}", event);
}
}
spring definition:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:eventbus="http://sargis.info/eventbus"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://sargis.info/eventbus http://sargis.info/eventbus/eventbus-spring-1.0.xsd">
<eventbus:handler-scan base-package="info.sargis.spring.eventbus" eventbus-ref="eventBus">
<eventbus:include type="assignable" expression="info.sargis.spring.eventbus.EventHandler"/>
<!-- We can use other filters as well
<eventbus:include type="annotation" expression="..."/>
<eventbus:include type="regex" expression="..."/>
<eventbus:include type="custom" expression="..."/>
-->
</eventbus:handler-scan>
<bean id="eventBus" class="com.google.common.eventbus.EventBus"/>
</beans>
test application:
public class GuavaEventBusTest {
public static void main(String[] args) {
GenericApplicationContext applicationContext = .... // Starting spring context
EventBus bean = applicationContext.getBean(EventBus.class);
bean.post("Hello Guava EventBus");
}
}
and I get the following output:
20:17:33.646 [main] INFO i.s.s.eventbus.AlertEventHandler - GOT EVENT: Hello Guava EventBus