Search in sources :

Example 1 with MotechListenerEventProxy

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");
                }
            }
        }
    }
}
Also used : MotechListenerNamedParametersProxy(org.motechproject.event.listener.annotations.MotechListenerNamedParametersProxy) MotechListenerAbstractProxy(org.motechproject.event.listener.annotations.MotechListenerAbstractProxy) MotechListener(org.motechproject.event.listener.annotations.MotechListener) Method(java.lang.reflect.Method) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy)

Example 2 with MotechListenerEventProxy

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());
}
Also used : MotechListenerNamedParametersProxy(org.motechproject.event.listener.annotations.MotechListenerNamedParametersProxy) MotechListenerAbstractProxy(org.motechproject.event.listener.annotations.MotechListenerAbstractProxy) ArrayList(java.util.ArrayList) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy) Test(org.junit.Test)

Example 3 with MotechListenerEventProxy

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);
}
Also used : MotechListenerNamedParametersProxy(org.motechproject.event.listener.annotations.MotechListenerNamedParametersProxy) MotechListenerAbstractProxy(org.motechproject.event.listener.annotations.MotechListenerAbstractProxy) ArrayList(java.util.ArrayList) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy) Test(org.junit.Test)

Example 4 with MotechListenerEventProxy

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);
    }
}
Also used : Method(java.lang.reflect.Method) EventListener(org.motechproject.event.listener.EventListener) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy)

Example 5 with MotechListenerEventProxy

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));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Method(java.lang.reflect.Method) ReflectionUtils.findMethod(org.springframework.util.ReflectionUtils.findMethod) EventListener(org.motechproject.event.listener.EventListener) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MotechListenerEventProxy (org.motechproject.event.listener.annotations.MotechListenerEventProxy)7 Test (org.junit.Test)5 EventListener (org.motechproject.event.listener.EventListener)4 Method (java.lang.reflect.Method)3 Matchers.anyString (org.mockito.Matchers.anyString)3 MotechListenerAbstractProxy (org.motechproject.event.listener.annotations.MotechListenerAbstractProxy)3 MotechListenerNamedParametersProxy (org.motechproject.event.listener.annotations.MotechListenerNamedParametersProxy)3 ArrayList (java.util.ArrayList)2 MotechEvent (org.motechproject.event.MotechEvent)2 HashSet (java.util.HashSet)1 MotechListener (org.motechproject.event.listener.annotations.MotechListener)1 ReflectionUtils.findMethod (org.springframework.util.ReflectionUtils.findMethod)1