Google Guava EventBus Pubisher Subscriber

Google Guava EventBus Pubisher Subscriber

Google Guava EventBus which allows communiation between componets in publish-subscriber without registering component with one another. It’s has been designed as an alternative approach for Java event process where explicit registration needed. Below I will show you very simple example with one publisher and two subscriber weather reporting.

Note: To user Google Guava you will need its jar which you can download latest verion form maven site here

As google don’t impose extending class or implement interface. It’s very simple to write publisher and subscriber as you see below:

  • Create subscriber1:
package com.javahonk.eventbus;

import com.google.common.eventbus.Subscribe;

public class Subscriber1 {

	public Integer temp;
	public String weatherType;
	public Integer getTemp() {
		return temp;
	}
	@Subscribe
	public void setTemp(Integer temp) {
		this.temp = temp;
	}
	public String getWeatherType() {
		return weatherType;
	}
	@Subscribe
	public void setWeatherType(String weatherType) {
		this.weatherType = weatherType;
	}
	

}
  • Create subscriber2:
package com.javahonk.eventbus;

import com.google.common.eventbus.Subscribe;

public class Subscriber2 {

	public Integer temp;
	public String weatherType;

	public Integer getTemp() {
		return temp;
	}

	@Subscribe
	public void setTemp(Integer temp) {
		this.temp = temp;
	}

	public String getWeatherType() {
		return weatherType;
	}

	@Subscribe
	public void setWeatherType(String weatherType) {
		this.weatherType = weatherType;
	}

}

In both subscriber class I have defined two variable temp and weathertype and its getter and setter. Only difference is our setter method have subscriber evnet with @
@Subscribe annotation.

  • Now let’s create Publisher class. For this I will create java main class which will act as publisher including printing subscriber event receive information.

WeatherReportPublisher.java:

package com.javahonk.eventbus;

import com.google.common.eventbus.EventBus;

public class WeatherReportPublisher {

	public static void main(String[] args) {
		
	    EventBus eventBus = new EventBus();
	    Subscriber1 subscriber1 = new Subscriber1();
	    Subscriber2 subscriber2 = new Subscriber2();

	    eventBus.register(subscriber1);
	    eventBus.register(subscriber2);

	    //Publish weather news
	    eventBus.post(30);
	    eventBus.post("Sunny");
	    
	    System.out.println("Subscriber 1 recieved message\n");
	    System.out.println("Today temp in Celsius: "+subscriber1.getTemp());
	    System.out.println("Today weather type: "+subscriber1.getWeatherType());
	    System.out.println();
	    
	    System.out.println("Subscriber 2 recieved message\n");
	    System.out.println("Today temp in Celsius: "+subscriber2.getTemp());
	    System.out.println("Today weather type: "+subscriber2.getWeatherType());

	   
	}

}
  • As you see in WeatherReportPublisher.java class we have instantiated EventBus and registered two subscribers. EventBus is publisher now whatever he will publish subscriber is listening so it will print out the result. If you run WeatherReportPublisher.java you will see below output:

2015-08-16 23_02_07-Java EE - TestFileLoadSpring_src_com_javahonk_eventbus_WeatherReportPublisher.ja

Leave a Reply

Your email address will not be published. Required fields are marked *