I know this is an old question but I think a more modern solution is to use Guava's Event Bus (granted I'm not sure if it will work for GWT but both your title and tags don't say GWT).
I actually have a custom RabbitMQ simple message container that will automatically create queue bindings and on messages received will then dispatch to Guava EventBus. Its incredible elegant, and scales superbly.
You can easily use your DI framework to register the Subscribers. For Spring I create BeanPostProcessor that will automatically registers beans with @Subscribe
.
Below is the Spring BeanPostProcessor:
package com.snaphop.spring;
import java.lang.reflect.Method;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@Component
public class EventBusBeanPostProcessor implements BeanPostProcessor {
private EventBus eventBus;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (isApplicable(bean)) {
eventBus.register(bean);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
protected boolean isApplicable(Object bean) {
for(Method m : bean.getClass().getMethods()) {
if (m.isAnnotationPresent(Subscribe.class))
return true;
}
return false;
}
@Autowired
public void setEventBus(EventBus eventBus) {
this.eventBus = eventBus;
}
}
I'm sure is trivial to do something similar in Guice.