Skip to content
This repository

HTTPS clone URL

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…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

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

guicified-eventbus

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='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd' xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
    <profiles>
        <profile>
            <repositories>
                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>bintray</name>
                    <url></url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>central</id>
                    <name>bintray-plugins</name>
                    <url></url>
                </pluginRepository>
            </pluginRepositories>
            <id>bintray</id>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>bintray</activeProfile>
    </activeProfiles>
</settings>

Example

package org.robotninjas.commandbus;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.inject.*;

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) {
            bus.register(Handler.class);
          }
        },
        MoreExecutors.sameThreadExecutor()));

    final CommandBus bus = injector.getInstance(CommandBus.class);
    bus.post(new Event1());
    bus.post(new Event2());
    bus.post(new Event3());
    bus.post(new Event4());
    bus.post(new 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.