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