Search in sources :

Example 1 with MotechListener

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);
    }
}
Also used : Level(org.motechproject.admin.messages.Level) DateTime(org.joda.time.DateTime) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 2 with MotechListener

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);
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 3 with MotechListener

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);
}
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 4 with MotechListener

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);
}
Also used : DateTime(org.joda.time.DateTime) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Example 5 with MotechListener

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

Aggregations

MotechListener (org.motechproject.event.listener.annotations.MotechListener)10 DateTime (org.joda.time.DateTime)3 Method (java.lang.reflect.Method)2 MotechEvent (org.motechproject.event.MotechEvent)2 Task (org.motechproject.tasks.domain.mds.task.Task)2 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)2 Transactional (org.springframework.transaction.annotation.Transactional)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Level (org.motechproject.admin.messages.Level)1 MotechListenerAbstractProxy (org.motechproject.event.listener.annotations.MotechListenerAbstractProxy)1 MotechListenerEventProxy (org.motechproject.event.listener.annotations.MotechListenerEventProxy)1 MotechListenerNamedParametersProxy (org.motechproject.event.listener.annotations.MotechListenerNamedParametersProxy)1 JobId (org.motechproject.scheduler.contract.JobId)1 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)1 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)1 MotechUser (org.motechproject.security.domain.MotechUser)1 Channel (org.motechproject.tasks.domain.mds.channel.Channel)1 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)1