Search in sources :

Example 21 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class TaskTriggerHandlerTest method shouldRegisterHandlerForSubject.

@Test
public void shouldRegisterHandlerForSubject() {
    String subject = "org.motechproject.messagecampaign.campaign-completed";
    handler.registerHandlerFor(subject);
    ArgumentCaptor<EventListener> captor = ArgumentCaptor.forClass(EventListener.class);
    verify(registryService).registerListener(captor.capture(), eq(subject));
    MotechListenerEventProxy proxy = (MotechListenerEventProxy) captor.getValue();
    assertEquals("taskTriggerHandler", proxy.getIdentifier());
    assertEquals(handler, proxy.getBean());
    assertEquals(findMethod(getTargetClass(handler), "handle", MotechEvent.class), proxy.getMethod());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) EventListener(org.motechproject.event.listener.EventListener) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 22 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class EventBundleIT method testEventListener.

@Test
public void testEventListener() throws Exception {
    final String subject = "OSGi IT - 001";
    final ArrayList<String> receivedEvents = new ArrayList<>();
    registry.registerListener(new EventListener() {

        @Override
        public void handle(MotechEvent event) {
            receivedEvents.add(event.getSubject());
            synchronized (waitLock) {
                waitLock.notify();
            }
        }

        @Override
        public String getIdentifier() {
            return subject;
        }
    }, subject);
    wait2s();
    eventRelay.sendEventMessage(new MotechEvent(subject));
    wait2s();
    assertEquals(1, receivedEvents.size());
    assertEquals(subject, receivedEvents.get(0));
}
Also used : ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 23 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class EventBundleIT method shouldProxyOSGiEvents.

@Test
public void shouldProxyOSGiEvents() throws InterruptedException {
    final ArrayList<MotechEvent> receivedEvents = new ArrayList<>();
    final String subject = "OSGi IT PROX";
    final DateTime now = DateUtil.now();
    final Map<String, Object> params = new HashMap<>();
    params.put("now", now);
    params.put("key", "value");
    registry.registerListener(new EventListener() {

        @Override
        public void handle(MotechEvent event) {
            receivedEvents.add(event);
            synchronized (waitLock) {
                waitLock.notify();
            }
        }

        @Override
        public String getIdentifier() {
            return subject;
        }
    }, subject);
    wait2s();
    osgiEventProxy.sendEvent(subject, params);
    wait2s();
    assertEquals(1, receivedEvents.size());
    MotechEvent event = receivedEvents.get(0);
    assertEquals(subject, event.getSubject());
    assertEquals(now, event.getParameters().get("now"));
    assertEquals("value", event.getParameters().get("key"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 24 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class ServerEventRelay method splitEvent.

/**
 * Split a given message into multiple messages with specific message destination
 * parameters. Message destinations will route the message to the specific message
 * listener.
 *
 * @param event     Event message to be split
 * @param listeners A list of listeners for this given message that will be used as message destinations
 */
private void splitEvent(MotechEvent event, Set<EventListener> listeners) {
    MotechEvent enrichedEventMessage;
    Map<String, Object> parameters;
    for (EventListener listener : listeners) {
        parameters = new HashMap<>();
        parameters.putAll(event.getParameters());
        enrichedEventMessage = new MotechEvent(event.getSubject(), parameters, event.getCallbackName());
        enrichedEventMessage.setMetadata(event.getMetadata());
        enrichedEventMessage.setMessageDestination(listener.getIdentifier());
        outboundEventGateway.sendEventMessage(enrichedEventMessage);
    }
}
Also used : EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent)

Example 25 with EventListener

use of org.motechproject.event.listener.EventListener in project motech by motech.

the class EventListenerTree method removeListeners.

private boolean removeListeners(String beanName) {
    boolean removed = false;
    if (listeners != null) {
        for (Iterator<EventListener> listenerIterator = listeners.iterator(); listenerIterator.hasNext(); ) {
            EventListener nextListener = listenerIterator.next();
            if (nextListener.getIdentifier().equals(beanName)) {
                listenerIterator.remove();
                removed = true;
            }
        }
    }
    if (wildcardListeners != null) {
        for (Iterator<EventListener> listenerIterator = wildcardListeners.iterator(); listenerIterator.hasNext(); ) {
            EventListener nextListener = listenerIterator.next();
            if (nextListener.getIdentifier().equals(beanName)) {
                listenerIterator.remove();
                removed = true;
            }
        }
    }
    for (EventListenerTree childTree : children) {
        if (childTree.removeListeners(beanName)) {
            removed = true;
        }
    }
    return removed;
}
Also used : EventListener(org.motechproject.event.listener.EventListener)

Aggregations

EventListener (org.motechproject.event.listener.EventListener)25 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)12 SampleEventListener (org.motechproject.event.listener.SampleEventListener)11 Matchers.anyString (org.mockito.Matchers.anyString)10 OtherSampleEventListener (org.motechproject.event.listener.OtherSampleEventListener)10 MotechEvent (org.motechproject.event.MotechEvent)9 MotechListenerEventProxy (org.motechproject.event.listener.annotations.MotechListenerEventProxy)4 HashMap (java.util.HashMap)3 Method (java.lang.reflect.Method)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DateTime (org.joda.time.DateTime)1 TestEventPayload (org.motechproject.event.domain.TestEventPayload)1 EventListenerTree (org.motechproject.event.listener.impl.EventListenerTree)1 TestMdsEntity (org.motechproject.mds.test.domain.TestMdsEntity)1 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)1 Task (org.motechproject.tasks.domain.mds.task.Task)1 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)1 TaskTriggerInformation (org.motechproject.tasks.domain.mds.task.TaskTriggerInformation)1