1   /**
2    * Copyright (c) 2000-2008 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              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 className)
87          throws SchedulerException {
88  
89          unscheduleJob(className + "_TRIGGER", Scheduler.DEFAULT_GROUP);
90      }
91  
92      public static void unscheduleJob(String triggerName, String groupName)
93          throws SchedulerException {
94  
95          _getScheduler().unscheduleJob(triggerName, groupName);
96      }
97  
98      private static Scheduler _getScheduler() {
99          return _instance._scheduler;
100     }
101 
102     private JobScheduler() {
103         _start();
104     }
105 
106     private void _start() {
107         StdSchedulerFactory sf = new StdSchedulerFactory();
108 
109         try {
110             _scheduler = sf.getScheduler();
111 
112             _scheduler.start();
113         }
114         catch (SchedulerException se) {
115             _log.error(se);
116         }
117     }
118 
119     private void _shutdown() {
120         try {
121             if (!_scheduler.isShutdown()) {
122                 _scheduler.shutdown();
123             }
124         }
125         catch (SchedulerException se) {
126             _log.error(se);
127         }
128     }
129 
130     private static Log _log = LogFactory.getLog(JobScheduler.class);
131 
132     private static JobScheduler _instance = new JobScheduler();
133 
134     private Scheduler _scheduler;
135 
136 }