View Javadoc

1   /**
2    * This file Copyright (c) 2006-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.scheduler;
35  
36  import info.magnolia.module.ModuleLifecycle;
37  import info.magnolia.module.ModuleLifecycleContext;
38  
39  import java.text.ParseException;
40  import java.util.ArrayList;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  import org.apache.commons.lang.StringUtils;
45  import org.quartz.CronTrigger;
46  import org.quartz.JobDetail;
47  import org.quartz.Scheduler;
48  import org.quartz.SchedulerException;
49  import org.quartz.SchedulerFactory;
50  import org.quartz.Trigger;
51  import org.quartz.impl.StdSchedulerFactory;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  
56  /**
57   * Scheduler module. Under the node jobs one can create jobs by defining the command to use. The scheduling is done
58   * based on a cron syntax scheduling expression. The job definition can contain a params node which can hold values
59   * passed to the command in the execution-context.
60   *
61   * @author philipp
62   * @version $Id: SchedulerModule.java 42534 2011-03-02 18:29:06Z gjoseph $
63   */
64  public class SchedulerModule implements ModuleLifecycle {
65      private static final Logger log = LoggerFactory.getLogger(JobDefinition.class);
66  
67      private static SchedulerModule instance;
68  
69      private List jobs = new ArrayList();
70  
71      private boolean running = false;
72  
73      /**
74       * The quartz scheduler.
75       */
76      protected Scheduler scheduler;
77  
78      public SchedulerModule() {
79          instance = this;
80      }
81  
82  
83      public List getJobs() {
84          return this.jobs;
85      }
86  
87  
88      public void setJobs(List jobs) {
89          this.jobs = jobs;
90      }
91  
92      public void addJob(JobDefinition job) throws SchedulerException{
93          jobs.add(job);
94          if(running){
95              initJob(job);
96          }
97      }
98  
99      /**
100      * Stops the scheduler.
101      */
102     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
103         try {
104             scheduler.shutdown(true);
105             running = false;
106         }
107         catch (SchedulerException e) {
108             log.error("Can't stop scheduler properly", e);
109         }
110     }
111 
112     /**
113      * Start scheduler and add jobs.
114      */
115     public void start(ModuleLifecycleContext moduleLifecycleContext) {
116         try {
117             initScheduler();
118             running = true;
119         }
120         catch (SchedulerException e) {
121             log.error("Can't start scheduler", e);
122             return;
123         }
124 
125         initJobs();
126     }
127 
128     /**
129      * Add all the jobs defined in the jobs node.
130      */
131     protected void initJobs() {
132         for (Iterator iter = jobs.iterator(); iter.hasNext();) {
133             JobDefinition job = (JobDefinition) iter.next();
134             try {
135                 initJob(job);
136             }
137             catch (SchedulerException e) {
138                 log.error("Can't initialize job [" + job.getName() + "]", e);
139             }
140         }
141     }
142 
143     /**
144      * Initialize a single job.
145      */
146     protected void initJob(JobDefinition job) throws SchedulerException {
147         if (job.isActive()) {
148             try {
149                 stopJob(job.getName());
150                 startJob(job);
151             }
152             catch (SchedulerException e) {
153                 throw new SchedulerException("Can't schedule job" + job.getName(), e);
154             }
155         }
156         else {
157             try {
158                stopJob(job.getName());
159             } catch (SchedulerException e) {
160                 throw new SchedulerException("Can't delete inactive job " + job.getName(), e);
161             }
162         }
163     }
164 
165     protected void startJob(JobDefinition job) throws SchedulerException {
166         Trigger trigger;
167         try {
168             String cron = cronToQuarzCron(job.getCron());
169             trigger = new CronTrigger(job.getName(), SchedulerConsts.SCHEDULER_GROUP_NAME, cron);
170         }
171         catch (ParseException e) {
172             log.error("Can't parse the job's cron expression [" + job.getCron() + "]", e);
173             return;
174         }
175 
176         final Class jobClass = job.isConcurrent() ? CommandJob.class : StatefulCommandJob.class;
177         final JobDetail jd = new JobDetail(job.getName(), SchedulerConsts.SCHEDULER_GROUP_NAME, jobClass);
178         jd.getJobDataMap().put(SchedulerConsts.CONFIG_JOB_COMMAND, job.getCommand());
179         jd.getJobDataMap().put(SchedulerConsts.CONFIG_JOB_COMMAND_CATALOG, job.getCatalog());
180         jd.getJobDataMap().put(SchedulerConsts.CONFIG_JOB_PARAMS, job.getParams());
181         scheduler.scheduleJob(jd, trigger);
182         log.info("Job " + job.getName() + " added [" + job.getCron() + "]. Will fire first time at " + trigger.getNextFireTime());
183     }
184 
185 
186     protected String cronToQuarzCron(String cron) {
187         // Quartz does not support * for both day of week and day of month. We simply handle this here.
188         String[] tokens = StringUtils.split(cron);
189         if (tokens.length >= 6) {
190             if (!tokens[3].equals("?") && !tokens[5].equals("?")) {
191                 if(tokens[5].equals("*")){
192                     tokens[5] = "?";
193                 }
194                 else if(tokens[3].equals("*")){
195                     tokens[3] = "?";
196                 }
197             }
198         }
199         cron = StringUtils.join(tokens, " ");
200         return cron;
201     }
202 
203     /**
204      * Deletes the job safely. If the job is not defined, the method does NOT throw an exception.
205      */
206     public void stopJob(String name) throws SchedulerException {
207         scheduler.deleteJob(name, SchedulerConsts.SCHEDULER_GROUP_NAME);
208     }
209 
210     /**
211      * Start the scheduler. No special configuration done yet.
212      * @throws SchedulerException
213      */
214     protected void initScheduler() throws SchedulerException {
215         SchedulerFactory sf = new StdSchedulerFactory();
216         scheduler = sf.getScheduler();
217         scheduler.start();
218     }
219 
220     /**
221      * If you need to get the scheduler handled by the module.
222      */
223     public Scheduler getScheduler() {
224         return scheduler;
225     }
226 
227     /**
228      * Singleton instance of the module.
229      */
230     public static SchedulerModule getInstance() {
231         return instance;
232     }
233 
234 }