Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An implementation of the Command Pattern using Guice and a copy of the Guava EventBus

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 src
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 pom.xml


An implementation of the Command Pattern using Guice and a copy of the Guava EventBus

This is a Proof-of-Concept implementation so there is no support or claim or usefulness in a production instance.

The basic flow is that you register a series of "commands" with the bus (classes with @Execute methods) and fire events on to it at some later point. When events are received by the bus the appropriate "command" class is created from the Guice injector, thus injecting it with all required fields, and the "@Execute" methods are invoked. Currently a new instance of the "command" is created for each method with an "@Execute" annotation. For instance, if a class has two methods annotated then one event will create two instances of that class and invoke each method separately on a separate class.

Get it

<?xml version='1.0' encoding='UTF-8'?>
<settings xsi:schemaLocation='' xmlns='' xmlns:xsi=''>


package org.robotninjas.commandbus;


import java.util.concurrent.atomic.AtomicInteger;

public class Test {

  public static void main(String[] args) {

    final AtomicInteger count = new AtomicInteger(0);

    final Injector injector = Guice.createInjector(
      new AbstractModule() {
        @Override protected void configure() {}
        @Provides public Integer getInt() {
          return count.getAndIncrement();
      new CommandBusModule(
        new CommandContext() {
          @Override public void configure(CommandBus bus) {

    final CommandBus bus = injector.getInstance(CommandBus.class); Event1()); Event2()); Event3()); Event4()); Event5());


  public static class Event1 {}
  public static class Event2 {}
  public static class Event3 {}
  public static class Event4 {}
  public static class Event5 {}

  public static class Handler {

    @Inject int count;

    @Execute public void handle(Event1 e) {
      System.out.println(e.getClass() + " " + count);

    @Execute public void handle(Event2 e) {
      System.out.println(e.getClass() + " " + count);

    @Execute public void handle(Event3 e) {
      System.out.println(e.getClass() + " " + count);

    @Execute public void handle(Event4 e) {
      System.out.println(e.getClass() + " " + count);

    @Execute public void handle(Event5 e) {
      System.out.println(e.getClass() + " " + count);



The output is, you'll notice by the count that each event is handled by it's own instance of Handler

class org.robotninjas.commandbus.Test$Event1 0
class org.robotninjas.commandbus.Test$Event2 1
class org.robotninjas.commandbus.Test$Event3 2
class org.robotninjas.commandbus.Test$Event4 3
class org.robotninjas.commandbus.Test$Event5 4
Something went wrong with that request. Please try again.