1 16 package org.apache.cocoon.components.cron; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.container.ContainerUtil; 21 import org.apache.avalon.framework.context.Context; 22 import org.apache.avalon.framework.logger.Logger; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.avalon.framework.service.ServiceManager; 25 import org.quartz.Job; 26 import org.quartz.JobDataMap; 27 import org.quartz.JobExecutionContext; 28 import org.quartz.JobExecutionException; 29 30 38 public class QuartzJobExecutor implements Job { 39 40 protected Logger m_logger; 41 protected Context m_context; 42 protected ServiceManager m_manager; 43 44 47 public void execute(final JobExecutionContext context) throws JobExecutionException { 48 49 final JobDataMap data = context.getJobDetail().getJobDataMap(); 50 51 final String name = (String ) data.get(QuartzJobScheduler.DATA_MAP_NAME); 52 53 m_logger = (Logger) data.get(QuartzJobScheduler.DATA_MAP_LOGGER); 54 m_context = (Context) data.get(QuartzJobScheduler.DATA_MAP_CONTEXT); 55 m_manager = (ServiceManager) data.get(QuartzJobScheduler.DATA_MAP_MANAGER); 56 57 final Boolean canRunConcurrentlyB = ((Boolean ) data.get(QuartzJobScheduler.DATA_MAP_RUN_CONCURRENT)); 58 final boolean canRunConcurrently = ((canRunConcurrentlyB == null) ? true : canRunConcurrentlyB.booleanValue()); 59 60 if (!canRunConcurrently) { 61 Boolean isRunning = (Boolean ) data.get(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING); 62 if (Boolean.TRUE.equals(isRunning)) { 63 m_logger.warn("Cron job name '" + name + 64 " already running but configured to not allow concurrent runs. Will discard this scheduled run"); 65 return; 66 } 67 } 68 69 if (m_logger.isInfoEnabled()) { 70 m_logger.info("Executing cron job named '" + name + "'"); 71 } 72 73 setup(data); 74 75 Object job = null; 76 String jobrole = null; 77 boolean release = false; 78 boolean dispose = false; 79 try { 80 jobrole = (String ) data.get(QuartzJobScheduler.DATA_MAP_ROLE); 81 82 if (null == jobrole) { 83 job = data.get(QuartzJobScheduler.DATA_MAP_OBJECT); 84 ContainerUtil.enableLogging(job, m_logger); 85 ContainerUtil.contextualize(job, m_context); 86 ContainerUtil.service(job, m_manager); 87 dispose = true; 88 } else { 89 job = m_manager.lookup(jobrole); 90 release = true; 91 } 92 93 if (job instanceof ConfigurableCronJob) { 94 final Parameters params = (Parameters) data.get(QuartzJobScheduler.DATA_MAP_PARAMETERS); 95 final Map objects = (Map ) data.get(QuartzJobScheduler.DATA_MAP_OBJECTMAP); 96 ((ConfigurableCronJob) job).setup(params, objects); 97 } 98 99 if (job instanceof Job) { 100 ((Job) job).execute(context); 101 } else if (job instanceof CronJob) { 102 ((CronJob) job).execute(name); 103 } else if (job instanceof Runnable ) { 104 ((Runnable ) job).run(); 105 } else { 106 m_logger.error("job named '" + name + "' is of invalid class: " + job.getClass().getName()); 107 } 108 } catch (final Throwable t) { 109 m_logger.error("Cron job name '" + name + "' died.", t); 110 111 if (t instanceof JobExecutionException) { 112 throw (JobExecutionException) t; 113 } 114 } finally { 115 116 release(data); 117 118 if (m_manager != null && release) { 119 m_manager.release(job); 120 } 121 if (dispose) { 122 ContainerUtil.dispose(job); 123 } 124 } 125 } 126 127 protected void setup(JobDataMap data) throws JobExecutionException { 128 data.put(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.TRUE); 129 } 130 131 protected void release(JobDataMap data) { 132 data.put(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.FALSE); 133 } 134 135 } 136 | Popular Tags |