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));
}
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"));
}
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);
}
}
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);
}
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);
}
}
Aggregations