Search in sources :

Example 6 with EventListener

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

the class EventListenerRegistryTest method testRegisterSingleListener.

@Test
public void testRegisterSingleListener() {
    EventListener sel = new SampleEventListener();
    registry.registerListener(sel, "org.motechproject.server.someevent");
    Set<EventListener> listeners = registry.getListeners("org.motechproject.server.someevent");
    assertNotNull(listeners);
    assertTrue(listeners.size() == 1);
    assertEquals(listeners.iterator().next(), sel);
}
Also used : OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) EventListener(org.motechproject.event.listener.EventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) Test(org.junit.Test)

Example 7 with EventListener

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

the class EventListenerRegistryTest method testRegisterTwice.

@Test
public void testRegisterTwice() {
    EventListener sel = new SampleEventListener();
    registry.registerListener(sel, "org.motechproject.server.someevent");
    registry.registerListener(sel, "org.motechproject.server.someevent");
    Set<EventListener> el = registry.getListeners("org.motechproject.server.someevent");
    assertNotNull(el);
    assertTrue(el.size() == 1);
    assertTrue(el.contains(sel));
}
Also used : OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) EventListener(org.motechproject.event.listener.EventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) OtherSampleEventListener(org.motechproject.event.listener.OtherSampleEventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) Test(org.junit.Test)

Example 8 with EventListener

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

the class EventBundleIT method testEventWithTypedPayloadAndMetadata.

@Test
public void testEventWithTypedPayloadAndMetadata() throws Exception {
    final ArrayList<MotechEvent> receivedEvents = new ArrayList<>();
    registry.registerListener(new EventListener() {

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

        @Override
        public String getIdentifier() {
            return "event";
        }
    }, "event");
    Map<String, Object> params = new HashMap<>();
    params.put("foo", new TestEventPayload());
    Map<String, Object> metadata = new HashMap<>();
    metadata.put("meta", new TestEventPayload());
    metadata.put("theNumberSeven", 7);
    wait2s();
    MotechEvent testEvent = new MotechEvent("event", params);
    testEvent.setMetadata(metadata);
    eventRelay.sendEventMessage(testEvent);
    wait2s();
    assertEquals(1, receivedEvents.size());
    MotechEvent receivedEvent = receivedEvents.get(0);
    assertTrue(receivedEvent.getParameters().get("foo") instanceof TestEventPayload);
    assertEquals(2, receivedEvent.getMetadata().size());
    assertTrue(receivedEvent.getMetadata().containsKey("meta"));
    assertTrue(receivedEvent.getMetadata().containsKey("theNumberSeven"));
    assertTrue(receivedEvent.getMetadata().get("meta") instanceof TestEventPayload);
    assertEquals(7, receivedEvent.getMetadata().get("theNumberSeven"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) TestEventPayload(org.motechproject.event.domain.TestEventPayload) Test(org.junit.Test)

Example 9 with EventListener

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

the class ServerEventRelay method relayTopicEvent.

/**
 * Relays the event that were published in the message topic to all listeners of that event.
 *
 * @param event the event being relayed
 */
public void relayTopicEvent(MotechEvent event) {
    verifyEventNotNull(event);
    Set<EventListener> listeners = getEventListeners(event);
    for (EventListener listener : listeners) {
        handleTopicEvent(listener, event);
    }
    // broadcast events can be also be additionally sent as OSGi events upon being received
    if (proxyInOsgi(event)) {
        sendInOSGi(event);
    }
}
Also used : EventListener(org.motechproject.event.listener.EventListener)

Example 10 with EventListener

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

the class ServerEventRelay method relayQueueEvent.

/**
 * Relays the event that were published in the message queue to all listeners of that event.
 *
 * @param event the event being relayed
 */
public void relayQueueEvent(MotechEvent event) {
    verifyEventNotNull(event);
    String messageDestination = event.getMessageDestination();
    if (null != messageDestination) {
        EventListener listener = getEventListener(event, messageDestination);
        if (null != listener) {
            MotechEvent e = copyMotechEvent(event);
            handleQueueEvent(listener, e);
        } else {
            LOGGER.warn("Event listener with identifier {} not present to handle the event: {}", messageDestination, event);
        }
    } else {
        LOGGER.warn("Message destination not present in event: {}", event);
    }
}
Also used : EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent)

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