Search in sources :

Example 46 with MotechEvent

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

the class StatusMessageServiceImpl method sendNotifications.

private void sendNotifications(StatusMessage message) {
    List<String> smsRecipients = new ArrayList<>();
    for (NotificationRule notificationRule : notificationRulesDataService.retrieveAll()) {
        if (notificationRule.matches(message)) {
            if (notificationRule.getActionType() == ActionType.SMS) {
                smsRecipients.add(notificationRule.getRecipient());
            } else if (notificationRule.getActionType() == ActionType.EMAIL) {
                try {
                    emailNotifier.send(message, notificationRule.getRecipient());
                } catch (EmailSendException e) {
                    LOGGER.error("Error while sending notification email to {}", notificationRule.getRecipient(), e);
                }
            }
        }
    }
    if (!smsRecipients.isEmpty()) {
        Map<String, Object> params = new HashMap<>();
        params.put("recipients", smsRecipients);
        params.put("message", String.format("Motech %s message: [%s] %s", message.getLevel(), message.getModuleName(), message.getText()));
        MotechEvent smsEvent = new MotechEvent("send_sms", params);
        eventRelay.sendEventMessage(smsEvent);
    }
}
Also used : HashMap(java.util.HashMap) NotificationRule(org.motechproject.admin.domain.NotificationRule) ArrayList(java.util.ArrayList) EmailSendException(org.motechproject.email.exception.EmailSendException) MotechEvent(org.motechproject.event.MotechEvent)

Example 47 with MotechEvent

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

the class MotechSchedulerListener method handleScheduleRepeatingJobEvent.

/**
 * Handles the motech event scheduling a new repeating job.
 *
 * @param event the event to be handled
 */
@MotechListener(subjects = { SCHEDULE_REPEATING_JOB })
public void handleScheduleRepeatingJobEvent(MotechEvent event) {
    Map<String, Object> parameters = event.getParameters();
    Map<String, Object> metadata = event.getMetadata();
    String jobSubject = (String) metadata.get(JOB_SUBJECT);
    Integer jobStart = (Integer) metadata.get(JOB_START);
    // Reset jobID that is appended with each retry and becomes too long
    parameters.put(JOB_ID, null);
    MotechEvent jobEvent = new MotechEvent(jobSubject, parameters, null, metadata);
    RunOnceSchedulableJob runOnceJob = new RunOnceSchedulableJob(jobEvent, DateTime.now().plusSeconds(jobStart));
    schedulerService.scheduleRunOnceJob(runOnceJob);
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 48 with MotechEvent

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

the class MotechSchedulerListener method handleUnscheduleRepeatingJobEvent.

/**
 * Handles the motech event unscheduling an existing repeating job.
 *
 * @param event the event to be handled
 */
@MotechListener(subjects = { UNSCHEDULE_REPEATING_JOB })
public void handleUnscheduleRepeatingJobEvent(MotechEvent event) {
    Map<String, Object> metadata = event.getMetadata();
    String jobSubject = (String) metadata.get(JOB_SUBJECT);
    MotechEvent jobEvent = new MotechEvent(jobSubject, null);
    JobId jobId = new RepeatingJobId(jobEvent);
    schedulerService.unscheduleJob(jobId);
}
Also used : RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechEvent(org.motechproject.event.MotechEvent) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 49 with MotechEvent

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

the class MotechScheduler method scheduleTestEvent.

private static void scheduleTestEvent() {
    Map<String, Object> params = new HashMap<>();
    params.put(MotechSchedulerService.JOB_ID_KEY, TEST_EVENT_NAME);
    MotechEvent motechEvent = new MotechEvent(TEST_SUBJECT, params);
    CronSchedulableJob cronSchedulableJob = new CronSchedulableJob(motechEvent, TEST_CRON_EXPRESSION);
    try {
        LOGGER.info("Scheduling test job: " + cronSchedulableJob);
        schedulerService.scheduleJob(cronSchedulableJob);
    } catch (RuntimeException e) {
        LOGGER.warn("Can not schedule test job.", e);
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent)

Example 50 with MotechEvent

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

the class ServerEventRelayBundleIT method shouldTriggerAllListenersIfOneListenerFailsWhenRelyingTopicEvent.

@Test
public void shouldTriggerAllListenersIfOneListenerFailsWhenRelyingTopicEvent() throws InterruptedException {
    TrackingListener buggyListener = new BuggyListener(1);
    TrackingListener firstGoodListener = new TrackingListener("first");
    TrackingListener secondGoodListener = new TrackingListener("second");
    eventListenerRegistry.registerListener(buggyListener, EXCEPTION_HANDLING_TEST);
    eventListenerRegistry.registerListener(firstGoodListener, EXCEPTION_HANDLING_TEST);
    eventListenerRegistry.registerListener(secondGoodListener, EXCEPTION_HANDLING_TEST);
    MotechEvent testMessage = new MotechEvent(EXCEPTION_HANDLING_TEST);
    eventRelay.broadcastEventMessage(testMessage);
    while (buggyListener.getCount() < 2) {
        Thread.sleep(1000);
    }
    Thread.sleep(2000);
    assertEquals(firstGoodListener.getCount(), 1);
    assertEquals(secondGoodListener.getCount(), 1);
}
Also used : BuggyListener(org.motechproject.event.domain.BuggyListener) TrackingListener(org.motechproject.event.domain.TrackingListener) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

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