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.portal.kernel.job.IntervalJob;
26  import com.liferay.portal.kernel.job.JobScheduler;
27  import com.liferay.util.Time;
28  
29  import java.util.Date;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import org.quartz.JobDetail;
35  import org.quartz.Scheduler;
36  import org.quartz.SimpleTrigger;
37  import org.quartz.Trigger;
38  import org.quartz.impl.StdSchedulerFactory;
39  
40  /**
41   * <a href="JobSchedulerImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class JobSchedulerImpl implements JobScheduler {
47  
48      public JobSchedulerImpl() {
49          StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
50  
51          try {
52              _scheduler = schedulerFactory.getScheduler();
53  
54              _scheduler.start();
55          }
56          catch (Exception e) {
57              _log.error(e);
58          }
59      }
60  
61      public void schedule(IntervalJob intervalJob) {
62          String jobName =
63              intervalJob.getClass().getName() + intervalJob.hashCode();
64  
65          JobClassUtil.put(jobName, (Class<IntervalJob>)intervalJob.getClass());
66  
67          Date startTime = new Date(System.currentTimeMillis() + Time.MINUTE * 1);
68          Date endTime = null;
69  
70          JobDetail jobDetail = new JobDetail(
71              jobName, Scheduler.DEFAULT_GROUP, JobWrapper.class);
72  
73          Trigger trigger = new SimpleTrigger(
74              jobName, Scheduler.DEFAULT_GROUP, startTime, endTime,
75              SimpleTrigger.REPEAT_INDEFINITELY, intervalJob.getInterval());
76  
77          try {
78              _scheduler.scheduleJob(jobDetail, trigger);
79          }
80          catch (Exception e) {
81              _log.error(e, e);
82          }
83      }
84  
85      public void shutdown() {
86          try {
87              if (!_scheduler.isShutdown()) {
88                  _scheduler.shutdown();
89              }
90          }
91          catch (Exception e) {
92              _log.error(e, e);
93          }
94      }
95  
96      public void unschedule(IntervalJob intervalJob) {
97          try {
98              String jobName =
99                  intervalJob.getClass().getName() + intervalJob.hashCode();
100 
101             _scheduler.unscheduleJob(jobName, Scheduler.DEFAULT_GROUP);
102         }
103         catch (Exception e) {
104             _log.error(e, e);
105         }
106     }
107 
108     private static Log _log = LogFactory.getLog(JobScheduler.class);
109 
110     private Scheduler _scheduler;
111 
112 }