Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active February 7, 2017 13:38
Show Gist options
  • Save antic183/67b9cd43c817fa6cad3774c59f6f661f to your computer and use it in GitHub Desktop.
Save antic183/67b9cd43c817fa6cad3774c59f6f661f to your computer and use it in GitHub Desktop.
google guava eventbus example
import com.google.common.eventbus.EventBus;
public class GoolgeGuavaEventBus
{
public static void main(String[] args) {
EventBus eventBus = new EventBus();
//register the receiver
Listener1 l1 = new Listener1();
Listener2 l2 = new Listener2();
eventBus.register(l1);
eventBus.register(l2);
// eventBus.unregister(l1); //unregister a receiver
eventBus.post("my message for Listener1 and Listener2!");
}
}
import com.google.common.eventbus.Subscribe;
public class Listener1
{
// identify post by param-signature
@Subscribe
public void doAnything(String s) {
System.out.println("receipt message in Listener1: '" + s + "'");
}
}
import com.google.common.eventbus.Subscribe;
public class Listener2
{
// identify post by param-signature
@Subscribe
public void doAlsoAnything(String s) {
System.out.println("receipt message in Listener2: '" + s + "'");
}
}
<!-- add dependencies -->
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment