1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.job;
24  
25  import com.liferay.util.Time;
26  
27  import java.util.Date;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import org.quartz.JobDetail;
33  import org.quartz.Scheduler;
34  import org.quartz.SchedulerException;
35  import org.quartz.SimpleTrigger;
36  import org.quartz.Trigger;
37  import org.quartz.impl.StdSchedulerFactory;
38  
39  /**
40   * <a href="JobScheduler.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class JobScheduler {
46  
47      public static void schedule(IntervalJob intervalJob)
48          throws SchedulerException {
49  
50          Date startTime = new Date(System.currentTimeMillis() + Time.MINUTE * 3);
51          Date endTime = null;
52  
53          JobDetail jobDetail = new JobDetail(
54              intervalJob.getClass().getName(), Scheduler.DEFAULT_GROUP,
55              intervalJob.getClass());
56  
57          Trigger trigger = new SimpleTrigger(
58              intervalJob.getClass().getName() + "_TRIGGER",
59              org.quartz.Scheduler.DEFAULT_GROUP, startTime, endTime,
60              SimpleTrigger.REPEAT_INDEFINITELY,
61              intervalJob.getInterval());
62  
63          scheduleJob(jobDetail, trigger);
64      }
65  
66      public static void scheduleJob(JobDetail jobDetail, Trigger trigger)
67          throws SchedulerException {
68  
69          _getScheduler().scheduleJob(jobDetail, trigger);
70      }
71  
72      public static void scheduleJob(Trigger trigger) throws SchedulerException {
73          _getScheduler().scheduleJob(trigger);
74      }
75  
76      public static void shutdown() {
77          _instance._shutdown();
78      }
79  
80      public static void triggerJob(String jobName, String groupName)
81          throws SchedulerException {
82  
83          _getScheduler().triggerJob(jobName, groupName);
84      }
85  
86      public static void unscheduleJob(String triggerName, String groupName)
87          throws SchedulerException {
88  
89          _getScheduler().unscheduleJob(triggerName, groupName);
90      }
91  
92      private static Scheduler _getScheduler() {
93          return _instance._scheduler;
94      }
95  
96      private JobScheduler() {
97          _start();
98      }
99  
100     private void _start() {
101         StdSchedulerFactory sf = new StdSchedulerFactory();
102 
103         try {
104             _scheduler = sf.getScheduler();
105             _scheduler.start();
106         }
107         catch (SchedulerException se) {
108             _log.error(se);
109         }
110     }
111 
112     private void _shutdown() {
113         try {
114             if (!_scheduler.isShutdown()) {
115                 _scheduler.shutdown();
116             }
117         }
118         catch (SchedulerException se) {
119             _log.error(se);
120         }
121     }
122 
123     private static Log _log = LogFactory.getLog(JobScheduler.class);
124 
125     private static JobScheduler _instance = new JobScheduler();
126 
127     private Scheduler _scheduler;
128 
129 }