Search in sources :

Example 11 with EventListener

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();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) TestMdsEntity(org.motechproject.mds.test.domain.TestMdsEntity) Test(org.junit.Test)

Example 12 with EventListener

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);
}
Also used : ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) Date(java.util.Date) Test(org.junit.Test)

Example 13 with EventListener

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));
}
Also used : EventListener(org.motechproject.event.listener.EventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) SampleEventListener(org.motechproject.event.listener.SampleEventListener) Test(org.junit.Test)

Example 14 with EventListener

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);
    }
}
Also used : Method(java.lang.reflect.Method) EventListener(org.motechproject.event.listener.EventListener) MotechListenerEventProxy(org.motechproject.event.listener.annotations.MotechListenerEventProxy)

Example 15 with EventListener

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);
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) 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)

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