use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.
the class MessageHandler method messageReceived.
/**
* Posts a status message using the {@link StatusMessageService}. The message is built from the event payload.
* This allows publishing status messages without a dependency on the Admin module.
* @param event the received event
* @see org.motechproject.admin.domain.StatusMessage
*/
@MotechListener(subjects = EventSubjects.MESSAGE_SUBJECT)
public void messageReceived(MotechEvent event) {
String message = (String) event.getParameters().get(EventKeys.MESSAGE);
String moduleName = (String) event.getParameters().get(EventKeys.MODULE_NAME);
String levelStr = (String) event.getParameters().get(EventKeys.LEVEL);
Level level = (StringUtils.isNotBlank(levelStr)) ? Level.valueOf(StringUtils.upperCase(levelStr)) : Level.INFO;
DateTime timeout = (DateTime) event.getParameters().get(EventKeys.TIMEOUT);
if (timeout == null) {
statusMessageService.postMessage(message, moduleName, level);
} else {
statusMessageService.postMessage(message, moduleName, level, timeout);
}
}
use of org.motechproject.event.listener.annotations.MotechListener 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.listener.annotations.MotechListener 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.listener.annotations.MotechListener in project motech by motech.
the class PurgeEmailEventHandlerImpl method handle.
@MotechListener(subjects = { PURGE_EMAIL_SUBJECT })
public void handle(MotechEvent event) {
String purgeTime = (String) event.getParameters().get("purgeTime");
String purgeMultiplier = (String) event.getParameters().get("purgeMultiplier");
DateTime deadline;
switch(purgeMultiplier) {
case "hours":
deadline = DateTime.now().toLocalDateTime().minusHours(Integer.parseInt(purgeTime)).toDateTime();
/* Using LocalDateTime, we make sure that a correct hour will be used in case of
change in the TimeZone */
break;
case "days":
deadline = DateTime.now().minusDays(Integer.parseInt(purgeTime));
break;
case "weeks":
deadline = DateTime.now().minusWeeks(Integer.parseInt(purgeTime));
break;
case "months":
deadline = DateTime.now().minusMonths(Integer.parseInt(purgeTime));
break;
// Fall through to the default value
case "years":
default:
deadline = DateTime.now().minusYears(Integer.parseInt(purgeTime));
break;
}
auditService.purgeEmailDeliveredBeforeDate(deadline);
}
use of org.motechproject.event.listener.annotations.MotechListener in project motech by motech.
the class EventAnnotationBeanPostProcessor method processAnnotations.
private void processAnnotations(final Object bean, final String beanName) {
if (bean == null) {
return;
}
// Get declared methods from class without superclass methods.
for (Method method : bean.getClass().getDeclaredMethods()) {
Method methodOfOriginalClassIfProxied = ReflectionUtils.findMethod(AopUtils.getTargetClass(bean), method.getName(), method.getParameterTypes());
if (methodOfOriginalClassIfProxied != null) {
MotechListener annotation = methodOfOriginalClassIfProxied.getAnnotation(MotechListener.class);
if (annotation != null) {
final List<String> subjects = Arrays.asList(annotation.subjects());
MotechListenerAbstractProxy proxy = null;
switch(annotation.type()) {
case MOTECH_EVENT:
proxy = new MotechListenerEventProxy(getFullyQualifiedBeanName(bean.getClass(), beanName), bean, method);
break;
case NAMED_PARAMETERS:
proxy = new MotechListenerNamedParametersProxy(getFullyQualifiedBeanName(bean.getClass(), beanName), bean, method);
break;
default:
}
LOGGER.info(String.format("Registering listener type(%20s) bean: %s, method: %s, for subjects: " + "%s", annotation.type().toString() + ":" + beanName, bean.getClass().getName(), method.toGenericString(), subjects));
if (eventListenerRegistry != null) {
eventListenerRegistry.registerListener(proxy, subjects);
} else {
LOGGER.error("Null eventListenerRegistry. Unable to register listener");
}
}
}
}
}
Aggregations