use of org.motechproject.event.listener.annotations.MotechListenerEventProxy in project motech by motech.
the class EventAnnotationBeanPostProcessor method processAnnotations.
private void processAnnotations(final Object bean, final String beanName) {
if (bean == null) {
return;
}
// Get declared methods from class without superclass methods.
for (Method method : bean.getClass().getDeclaredMethods()) {
Method methodOfOriginalClassIfProxied = ReflectionUtils.findMethod(AopUtils.getTargetClass(bean), method.getName(), method.getParameterTypes());
if (methodOfOriginalClassIfProxied != null) {
MotechListener annotation = methodOfOriginalClassIfProxied.getAnnotation(MotechListener.class);
if (annotation != null) {
final List<String> subjects = Arrays.asList(annotation.subjects());
MotechListenerAbstractProxy proxy = null;
switch(annotation.type()) {
case MOTECH_EVENT:
proxy = new MotechListenerEventProxy(getFullyQualifiedBeanName(bean.getClass(), beanName), bean, method);
break;
case NAMED_PARAMETERS:
proxy = new MotechListenerNamedParametersProxy(getFullyQualifiedBeanName(bean.getClass(), beanName), bean, method);
break;
default:
}
LOGGER.info(String.format("Registering listener type(%20s) bean: %s, method: %s, for subjects: " + "%s", annotation.type().toString() + ":" + beanName, bean.getClass().getName(), method.toGenericString(), subjects));
if (eventListenerRegistry != null) {
eventListenerRegistry.registerListener(proxy, subjects);
} else {
LOGGER.error("Null eventListenerRegistry. Unable to register listener");
}
}
}
}
}
use of org.motechproject.event.listener.annotations.MotechListenerEventProxy in project motech by motech.
the class HandlerPredicatesTest method shouldFoundCorrectObject.
@Test
public void shouldFoundCorrectObject() {
MotechListenerEventProxy expected = new MotechListenerEventProxy("def", null, null);
List<MotechListenerAbstractProxy> list = new ArrayList<>();
list.add(new MotechListenerNamedParametersProxy("abc", null, null));
list.add(expected);
list.add(new MotechListenerEventProxy("ghi", null, null));
MotechListenerAbstractProxy actual = (MotechListenerAbstractProxy) find(list, HandlerPredicates.withServiceName("def"));
assertTrue(actual instanceof MotechListenerEventProxy);
assertEquals(expected.getIdentifier(), actual.getIdentifier());
}
use of org.motechproject.event.listener.annotations.MotechListenerEventProxy in project motech by motech.
the class HandlerPredicatesTest method shouldNotFoundCorrectObject.
@Test
public void shouldNotFoundCorrectObject() {
List<MotechListenerAbstractProxy> list = new ArrayList<>();
list.add(new MotechListenerNamedParametersProxy("abc", null, null));
list.add(new MotechListenerEventProxy("def", null, null));
list.add(new MotechListenerEventProxy("ghi", null, null));
MotechListenerAbstractProxy actual = (MotechListenerAbstractProxy) find(list, HandlerPredicates.withServiceName("abc"));
assertNull(actual);
}
use of org.motechproject.event.listener.annotations.MotechListenerEventProxy in project motech by motech.
the class TaskTriggerHandler method registerHandlerFor.
@Override
public void registerHandlerFor(String subject, boolean isRetryHandler) {
LOGGER.info("Registering handler for {}", subject);
String methodName = isRetryHandler ? "handleRetry" : "handle";
Method method = ReflectionUtils.findMethod(this.getClass(), methodName, MotechEvent.class);
Object obj = CollectionUtils.find(registryService.getListeners(subject), withServiceName(BEAN_NAME));
try {
if (method != null && obj == null) {
EventListener proxy = new MotechListenerEventProxy(BEAN_NAME, this, method);
registryService.registerListener(proxy, subject);
LOGGER.info(String.format("%s listens on subject %s", BEAN_NAME, subject));
}
} catch (RuntimeException exp) {
LOGGER.error(String.format("%s can not listen on subject %s due to:", BEAN_NAME, subject), exp);
}
}
use of org.motechproject.event.listener.annotations.MotechListenerEventProxy in project motech by motech.
the class TaskTriggerHandlerTest method shouldRegisterHandlerOneTimeForSameSubjects.
@Test
public void shouldRegisterHandlerOneTimeForSameSubjects() {
String subject = "org.motechproject.messagecampaign.campaign-completed";
Method method = findMethod(getTargetClass(handler), "handle", MotechEvent.class);
Set<EventListener> listeners = new HashSet<>();
listeners.add(new MotechListenerEventProxy("taskTriggerHandler", this, method));
when(registryService.getListeners(subject)).thenReturn(listeners);
handler.registerHandlerFor(subject);
handler.registerHandlerFor(subject);
handler.registerHandlerFor(subject);
handler.registerHandlerFor(subject);
handler.registerHandlerFor(subject);
handler.registerHandlerFor(subject);
handler.registerHandlerFor(subject);
verify(registryService, never()).registerListener(any(EventListener.class), eq(subject));
}
Aggregations