Search in sources :

Example 1 with MotechOsgiConfigurableApplicationContext

use of org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext in project motech by motech.

the class MdsScheduledJob method execute.

@Override
@SuppressWarnings("unchecked")
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    LOGGER.info("Executing Trash Clean Up");
    try {
        SchedulerContext schedulerContext;
        try {
            schedulerContext = jobExecutionContext.getScheduler().getContext();
        } catch (SchedulerException e) {
            LOGGER.error("Can not execute job. Can not get Scheduler Context", e);
            return;
        }
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get("applicationContext");
        BundleContext bundleContext = ((MotechOsgiConfigurableApplicationContext) applicationContext).getBundleContext();
        TrashService trashService = OSGiServiceUtils.findService(bundleContext, TrashService.class);
        EntityInfoReader entityInfoReader = OSGiServiceUtils.findService(bundleContext, EntityInfoReader.class);
        if (trashService != null) {
            if (entityInfoReader != null) {
                Collection<String> entitiesClassNames = entityInfoReader.getEntitiesClassNames();
                trashService.emptyTrash(entitiesClassNames);
            } else {
                LOGGER.warn("EntityInfoReader is unavailable, unable to empty trash");
            }
        } else {
            LOGGER.warn("TrashService is unavailable, unable to empty trash");
        }
    } catch (Exception e) {
        LOGGER.error("Job execution failed.", e);
    }
}
Also used : MotechOsgiConfigurableApplicationContext(org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SchedulerException(org.quartz.SchedulerException) MotechOsgiConfigurableApplicationContext(org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext) TrashService(org.motechproject.mds.service.TrashService) SchedulerContext(org.quartz.SchedulerContext) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) BundleContext(org.osgi.framework.BundleContext) EntityInfoReader(org.motechproject.mds.entityinfo.EntityInfoReader)

Aggregations

MotechOsgiConfigurableApplicationContext (org.motechproject.bundle.extender.MotechOsgiConfigurableApplicationContext)1 EntityInfoReader (org.motechproject.mds.entityinfo.EntityInfoReader)1 TrashService (org.motechproject.mds.service.TrashService)1 BundleContext (org.osgi.framework.BundleContext)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerContext (org.quartz.SchedulerContext)1 SchedulerException (org.quartz.SchedulerException)1 ApplicationContext (org.springframework.context.ApplicationContext)1