Search in sources :

Example 11 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class EventBundleIT method testEventListenerWithAnnotation.

@Test
public void testEventListenerWithAnnotation() throws Exception {
    final TestEventListenerOsgi testEventListenerOsgi = (TestEventListenerOsgi) ServiceRetriever.getWebAppContext(bundleContext, bundleContext.getBundle().getSymbolicName()).getBean("testEventListenerOsgi");
    eventRelay.sendEventMessage(new MotechEvent(TestEventListenerOsgi.TEST_SUBJECT_OSGI));
    final List<String> receivedEvents = testEventListenerOsgi.getReceivedEvents();
    synchronized (receivedEvents) {
        receivedEvents.wait(2000);
    }
    assertEquals(1, receivedEvents.size());
    assertEquals(TestEventListenerOsgi.TEST_SUBJECT_OSGI, receivedEvents.get(0));
}
Also used : TestEventListenerOsgi(org.motechproject.event.osgi.TestEventListenerOsgi) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 12 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class EventRelayClassLoaderBundleIT method testThatEventHandlerClassLoaderIsInvokedWithCurrentClassLoaderSetAsEventRelaysClassLoader.

@Test
public void testThatEventHandlerClassLoaderIsInvokedWithCurrentClassLoaderSetAsEventRelaysClassLoader() throws InterruptedException {
    assertNotNull(eventListenerRegistry);
    assertNotNull(eventRelay);
    new Wait(new ContextPublishedWaitCondition(bundleContext, "org.motechproject.motech-platform-event"), 5000).start();
    eventRelay.sendEventMessage(new MotechEvent(TestHandler.SUBJECT_READ));
    final Properties properties = TestHandler.PROPERTIES;
    new Wait(new WaitCondition() {

        @Override
        public boolean needsToWait() {
            return properties.isEmpty();
        }
    }, 2000).start();
    assertTrue(properties.containsKey("message"));
    assertEquals("hello world", properties.get("message"));
}
Also used : WaitCondition(org.motechproject.testing.osgi.wait.WaitCondition) ContextPublishedWaitCondition(org.motechproject.testing.osgi.wait.ContextPublishedWaitCondition) ContextPublishedWaitCondition(org.motechproject.testing.osgi.wait.ContextPublishedWaitCondition) Wait(org.motechproject.testing.osgi.wait.Wait) Properties(java.util.Properties) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 13 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class ServerEventRelay method handleEvent.

/**
 * Receives an OSGi event with the proxy topic. This event is then proxied as a regular Motech event.
 * This allows sending Motech events without having a dependency on Event itself -  which is used by MDS.
 * Unless necessary using the proxy mechanism should be avoided.
 *
 * @param osgiEvent the OSGi event to be proxied
 */
@Override
public void handleEvent(Event osgiEvent) {
    String subject = (String) osgiEvent.getProperty(OsgiEventProxy.SUBJECT_PARAM);
    Map<String, Object> parameters = (Map<String, Object>) osgiEvent.getProperty(OsgiEventProxy.PARAMETERS_PARAM);
    Boolean broadcast = (Boolean) osgiEvent.getProperty(OsgiEventProxy.BROADCAST_PARAM);
    Boolean proxyOnReceivingEnd = (Boolean) osgiEvent.getProperty(OsgiEventProxy.PROXY_ON_RECEIVING_END_PARAM);
    LOGGER.debug("Relying OSGi event - subject: {}, broadcast: {}, proxyWhenReceiving: {}", subject, broadcast, proxyOnReceivingEnd);
    if (parameters == null) {
        parameters = new HashMap<>();
    }
    // OSGi events are local to their OSGi framework (MOTECH instance)
    if (proxyOnReceivingEnd != null && proxyOnReceivingEnd) {
        parameters.put(PROXY_IN_OSGI, true);
    }
    MotechEvent motechEvent = new MotechEvent(subject, parameters);
    if (broadcast != null && broadcast) {
        broadcastEventMessage(motechEvent);
    } else {
        sendEventMessage(motechEvent);
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) MotechEvent(org.motechproject.event.MotechEvent)

Example 14 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class ServerEventRelay method sendInOSGi.

private void sendInOSGi(MotechEvent motechEvent) {
    Event osgiEvent = new Event(motechEvent.getSubject(), motechEvent.getParameters());
    osgiEventAdmin.postEvent(osgiEvent);
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) Event(org.osgi.service.event.Event)

Example 15 with MotechEvent

use of org.motechproject.event.MotechEvent 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

MotechEvent (org.motechproject.event.MotechEvent)138 Test (org.junit.Test)87 HashMap (java.util.HashMap)79 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)28 DateTime (org.joda.time.DateTime)25 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)20 ArrayList (java.util.ArrayList)14 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)13 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)11 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)8 Matchers.anyString (org.mockito.Matchers.anyString)7 EventListener (org.motechproject.event.listener.EventListener)7 CronJobId (org.motechproject.scheduler.contract.CronJobId)7 Task (org.motechproject.tasks.domain.mds.task.Task)7 JobId (org.motechproject.scheduler.contract.JobId)6 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)6 Time (org.motechproject.commons.date.model.Time)5 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)5 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)5