Skip to content

Commit

Permalink
make dispatcher eagerSingleton
Browse files Browse the repository at this point in the history
polish javadoc and exception handling
  • Loading branch information
Taylor Wicksell committed Feb 18, 2016
1 parent bca40b2 commit 064a632
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
Expand Up @@ -2,7 +2,8 @@

/**
* Marker interface for application events to be published
* to subscribed consumers.
* to subscribed consumers. See {@link ApplicationEventDispatcher} and {@link EventListener}
* for examples of publishing and subscribing to events.
*/
public interface ApplicationEvent {

Expand Down
Expand Up @@ -46,7 +46,8 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
@Override
public void afterInjection(Object injectee) {
for (final Method handlerMethod : handlerMethods) {
dispatcherProvider.get().registerListener(injectee, handlerMethod, (Class<? extends ApplicationEvent>) handlerMethod.getParameterTypes()[0]);
dispatcherProvider.get().registerListener(injectee, handlerMethod,
(Class<? extends ApplicationEvent>) handlerMethod.getParameterTypes()[0]);
}
}
});
Expand All @@ -63,10 +64,9 @@ private List<Method> getAllDeclaredHandlerMethods(Class<?> clazz) {
&& ApplicationEvent.class.isAssignableFrom(handlerMethod.getParameterTypes()[0])) {
handlerMethods.add(handlerMethod);
} else {
LOG.warn(
"@EventListener {}.{} skipped. Methods must be public, void, and accept exactly"
+ " one argument extending com.netflix.governator.event.ApplicationEvent.",
clazz.getName(), handlerMethod.getName());
throw new IllegalArgumentException("@EventListener " + clazz.getName() + "." + handlerMethod.getName()
+ "skipped. Methods must be public, void, and accept exactly"
+ " one argument extending com.netflix.governator.event.ApplicationEvent.");
}
}
}
Expand Down
Expand Up @@ -5,29 +5,24 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.inject.Inject;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.common.reflect.TypeToken;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.netflix.governator.event.ApplicationEvent;
import com.netflix.governator.event.ApplicationEventDispatcher;
import com.netflix.governator.event.ApplicationEventListener;
import com.netflix.governator.event.ApplicationEventModule;
import com.netflix.governator.event.ApplicationEventRegistration;

public final class GuavaApplicationEventModule extends AbstractModule {

@Provides
@Singleton
public ApplicationEventDispatcher dispatcher() {
return new GuavaApplicationEventDispatcher(new EventBus("Governator Application Event Bus"));
}


@Override
protected void configure() {
install(new ApplicationEventModule());
bind(ApplicationEventDispatcher.class).to(GuavaApplicationEventDispatcher.class).asEagerSingleton();
}

@Override
Expand All @@ -50,6 +45,7 @@ private static final class GuavaApplicationEventDispatcher implements Applicatio
private final EventBus eventBus;
private final Method eventListenerMethod;

@Inject
public GuavaApplicationEventDispatcher(EventBus eventBus) {
this.eventBus = eventBus;
try {
Expand Down

0 comments on commit 064a632

Please sign in to comment.