1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ServerDetector;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Time;
32  
33  import java.util.Date;
34  
35  import org.quartz.JobDetail;
36  import org.quartz.Scheduler;
37  import org.quartz.SimpleTrigger;
38  import org.quartz.Trigger;
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 void schedule(IntervalJob intervalJob) {
49          if (intervalJob == null) {
50              return;
51          }
52  
53          try {
54              if (_scheduler.isShutdown()) {
55                  return;
56              }
57          }
58          catch (Exception e) {
59              _log.error(e, e);
60          }
61  
62          String jobName =
63              intervalJob.getClass().getName() + StringPool.AT +
64                  intervalJob.hashCode();
65  
66          JobClassUtil.put(jobName, (Class<IntervalJob>)intervalJob.getClass());
67  
68          Date startTime = null;
69  
70          try {
71              if (ServerDetector.getServerId().equals(ServerDetector.TOMCAT_ID)) {
72                  startTime = new Date(System.currentTimeMillis() + Time.MINUTE);
73              }
74          }
75          catch (RuntimeException re) {
76  
77              // ServerDetector will throw an exception when JobSchedulerImpl is
78              // initialized in a test environment
79  
80          }
81  
82          if (startTime == null) {
83              startTime = new Date(System.currentTimeMillis() + Time.MINUTE * 3);
84          }
85  
86          Date endTime = null;
87  
88          JobDetail jobDetail = new JobDetail(
89              jobName, Scheduler.DEFAULT_GROUP, JobWrapper.class);
90  
91          Trigger trigger = new SimpleTrigger(
92              jobName, Scheduler.DEFAULT_GROUP, startTime, endTime,
93              SimpleTrigger.REPEAT_INDEFINITELY, intervalJob.getInterval());
94  
95          try {
96              _scheduler.scheduleJob(jobDetail, trigger);
97          }
98          catch (Exception e) {
99              _log.error(e, e);
100         }
101     }
102 
103     public void setScheduler(Scheduler scheduler) {
104         _scheduler = scheduler;
105     }
106 
107     public void shutdown() {
108         try {
109             if (!_scheduler.isShutdown()) {
110                 _scheduler.shutdown();
111             }
112         }
113         catch (Exception e) {
114             _log.error(e, e);
115         }
116     }
117 
118     public void unschedule(IntervalJob intervalJob) {
119         if (intervalJob == null) {
120             return;
121         }
122 
123         try {
124             if (_scheduler.isShutdown()) {
125                 return;
126             }
127         }
128         catch (Exception e) {
129             _log.error(e, e);
130         }
131 
132         try {
133             String jobName =
134                 intervalJob.getClass().getName() + StringPool.AT +
135                     intervalJob.hashCode();
136 
137             _scheduler.unscheduleJob(jobName, Scheduler.DEFAULT_GROUP);
138         }
139         catch (Exception e) {
140             _log.error(e, e);
141         }
142     }
143 
144     private static Log _log = LogFactoryUtil.getLog(JobScheduler.class);
145 
146     private Scheduler _scheduler;
147 
148 }