KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > cron > QuartzJobExecutor


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.cron;
17
18 import java.util.Map JavaDoc;
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 /**
31  * This component is resposible to launch a {@link CronJob}s in a Quart Scheduler.
32  *
33  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
34  * @version CVS $Id: QuartzJobExecutor.java 54673 2004-10-12 15:42:11Z unico $
35  *
36  * @since 2.1.1
37  */

38 public class QuartzJobExecutor implements Job {
39
40     protected Logger m_logger;
41     protected Context m_context;
42     protected ServiceManager m_manager;
43
44     /* (non-Javadoc)
45      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
46      */

47     public void execute(final JobExecutionContext context) throws JobExecutionException {
48
49         final JobDataMap data = context.getJobDetail().getJobDataMap();
50
51         final String JavaDoc name = (String JavaDoc) 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 JavaDoc canRunConcurrentlyB = ((Boolean JavaDoc) data.get(QuartzJobScheduler.DATA_MAP_RUN_CONCURRENT));
58         final boolean canRunConcurrently = ((canRunConcurrentlyB == null) ? true : canRunConcurrentlyB.booleanValue());
59
60         if (!canRunConcurrently) {
61             Boolean JavaDoc isRunning = (Boolean JavaDoc) 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 JavaDoc job = null;
76         String JavaDoc jobrole = null;
77         boolean release = false;
78         boolean dispose = false;
79         try {
80             jobrole = (String JavaDoc) 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 JavaDoc objects = (Map JavaDoc) 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 JavaDoc) {
104                 ((Runnable JavaDoc) job).run();
105             } else {
106                 m_logger.error("job named '" + name + "' is of invalid class: " + job.getClass().getName());
107             }
108         } catch (final Throwable JavaDoc 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