Guava EventBus Spring
In last two tutorial you saw how to get started with Google Guava EventBus. In this I will show you how to integrate Guava EventBus with Spring framework. If you are seriously looking to integrate Guava EventBus to your project then I will suggest read my next Guava EventBus Spring advanced integration tutorial. Let’s get started and see first how to integrate EventBus with Spring.
- Create maven project name: EventBusSpringIntegration as below shown complete project structure:
As above you see we have main class which loads application context and two subscriber including DeadEventProcessor class where you can catch any unprocessed event.
- pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javahonk</groupId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>EventBusSpringIntegration</name> <url>http://maven.apache.org</url> <properties> <org.springframework.version>4.1.5.RELEASE</org.springframework.version> <log4j.version>1.2.16</log4j.version> <org.apache.log4j.version>2.1</org.apache.log4j.version> <guava.version>18.0</guava.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Log4j --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${org.apache.log4j.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${org.apache.log4j.version}</version> </dependency> <!-- Guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> </dependencies> <artifactId>EventBusSpringIntegration</artifactId> </project>
- DeadEventProcessor.java: This class has been written to find if any event which got posted not have any subscriber. You could put debug point to see any event not processed caught here as I have included one event for testing:
package com.javahonk.subscriber; import javax.annotation.PostConstruct; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import com.google.common.eventbus.DeadEvent; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; public class DeadEventProcessor { private static final Logger logger = LogManager.getLogger(DeadEventProcessor.class.getName()); @Autowired EventBus eventBus; @PostConstruct public void init(){ eventBus.register(this); } @Subscribe public void processDeadEvent(DeadEvent deadEvent){ logger.error("DEADEVENT DETECTED:{}",deadEvent.getEvent().getClass()); } }
- Subscriber1.java:
package com.javahonk.subscriber; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; public class Subscriber1 { @Autowired EventBus eventBus; @PostConstruct public void init(){ eventBus.register(this); } @Subscribe public void getPublishData(SendPostThourghClass sendPostThourghClass){ System.out.println("Got post data on Subscriber1: "+sendPostThourghClass.getName()); } }
- Subscriber2.java:
package com.javahonk.subscriber; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; public class Subscriber2 { @Autowired EventBus eventBus; @PostConstruct public void init(){ eventBus.register(this); } @Subscribe public void getPublishData(SendPostThourghClass sendPostThourghClass){ System.out.println("Got post data on Subscriber2: "+sendPostThourghClass.getName()); } }
SendPostThourghClass.java: This is sample class just to show how you could use classes to post event otherwise you could post any object as event:
- SendPostThourghClass.java:
package com.javahonk.subscriber; public class SendPostThourghClass { private String name; public SendPostThourghClass(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- spring-context.xml: Main context file where you define all your configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:annotation-config /> <context:component-scan base-package="com.javahonk, com.javahonk.eventbusprocessor, com.javahonk.subscriber" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>application.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean> <bean id="eventBus" class="com.google.common.eventbus.EventBus" /> <bean id="deadEventProcessor" class="com.javahonk.subscriber.DeadEventProcessor" /> <bean id="subscriber1" class="com.javahonk.subscriber.Subscriber1" /> <bean id="subscriber2" class="com.javahonk.subscriber.Subscriber2" /> </beans>
- MainApplication.java: Main class to test our application:
package com.javahonk; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.google.common.eventbus.EventBus; import com.javahonk.subscriber.SendPostThourghClass; public class MainApplication { private static final Logger logger = LogManager.getLogger(MainApplication.class.getName()); public static void main(String[] args) { logger.info("EventBus Spring application starting..."); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml"); EventBus eventBus = (EventBus)applicationContext.getBean(EventBus.class); eventBus.post("This event will go to DeadEventProcessor because no subscriber"); eventBus.post( new SendPostThourghClass ("This will go to subscribers")); ((AbstractApplicationContext)applicationContext).close(); } }
- That’s it. To validate run MainApplication.java as java application you will see below output:
- For more information please visit Google Guava EventBus official site here
Download Project: EventBusSpringIntegration