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