use of org.motechproject.event.listener.EventListener in project motech by motech.
the class MdsDdeBundleIT method testMdsCrudEvents.
@Test
public void testMdsCrudEvents() throws Exception {
getLogger().info("Test MDS CRUD Events");
final ArrayList<String> receivedEvents = new ArrayList<>();
final Map<String, Object> params = new HashMap<>();
String moduleName = "MOTECH Platform Data Services Test Bundle";
String simplifiedModuleName = simplifiedModuleName(moduleName);
String entityName = "TestMdsEntity";
final String subject = createSubject(moduleName, null, entityName, CrudEventType.CREATE);
registry.registerListener(new EventListener() {
@Override
public void handle(MotechEvent event) {
receivedEvents.add(event.getSubject());
params.putAll(event.getParameters());
synchronized (waitLock) {
waitLock.notify();
}
}
@Override
public String getIdentifier() {
return subject;
}
}, subject);
wait2s();
testMdsEntityService.create(new TestMdsEntity("string"));
wait2s();
assertEquals(1, receivedEvents.size());
assertEquals(subject, receivedEvents.get(0));
assertEquals(simplifiedModuleName, params.get(MODULE_NAME));
assertEquals(entityName, params.get(ENTITY_NAME));
assertEquals(TestMdsEntity.class.getName(), params.get(ENTITY_CLASS));
testMdsEntityService.deleteAll();
}
use of org.motechproject.event.listener.EventListener in project motech by motech.
the class SchedulerBundleIT method testRunOnceJob.
@Test
public void testRunOnceJob() throws InterruptedException {
waitForEventConsumerToStart();
final List<String> receivedEvents = new ArrayList<>();
eventRegistry.registerListener(new EventListener() {
@Override
public void handle(MotechEvent event) {
synchronized (receivedEvents) {
receivedEvents.add(event.getSubject());
receivedEvents.notify();
}
}
@Override
public String getIdentifier() {
return TEST_SUBJECT;
}
}, TEST_SUBJECT);
final MotechEvent motechEvent = new MotechEvent(TEST_SUBJECT);
motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, "jobId");
schedulerService.unscheduleAllJobs("SchedulerBundleIT");
schedulerService.scheduleRunOnceJob(new RunOnceSchedulableJob(motechEvent, DateTime.now().plusSeconds(5)));
synchronized (receivedEvents) {
System.out.print("\nEvent waiting " + new Date() + "\n");
receivedEvents.wait(15000);
}
assertEquals(1, receivedEvents.size());
assertEquals(receivedEvents.get(0), TEST_SUBJECT);
}
use of org.motechproject.event.listener.EventListener in project motech by motech.
the class EventListenerTreeTest method testAddListener_UniversalListener.
@Test
public void testAddListener_UniversalListener() {
EventListener listener1 = new SampleEventListener();
EventListener listener2 = new SampleEventListener();
tree.addListener(listener1, "*");
tree.addListener(listener2, "org.test");
Set<EventListener> listeners = tree.getListeners("*");
assertTrue(listeners.size() == 1);
assertTrue(listeners.contains(listener1));
listeners = tree.getListeners("org.test");
assertTrue(listeners.size() == 2);
assertTrue(listeners.contains(listener1));
assertTrue(listeners.contains(listener2));
listeners = tree.getListeners("com.pqr.xyz");
assertTrue(listeners.size() == 1);
assertTrue(listeners.contains(listener1));
}
use of org.motechproject.event.listener.EventListener 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);
}
}
use of org.motechproject.event.listener.EventListener in project motech by motech.
the class EventListenerRegistryTest method testGetListeners.
@Test
public void testGetListeners() {
List<String> et = new ArrayList<String>();
et.add("org.motechproject.server.someevent");
EventListener sel = new SampleEventListener();
registry.registerListener(sel, et);
Set<EventListener> el = registry.getListeners("org.motechproject.server.someevent");
assertNotNull(el);
assertEquals(1, el.size());
assertEquals(el.iterator().next(), sel);
}
Aggregations