1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.job;
21  
22  import com.liferay.portal.kernel.job.IntervalJob;
23  import com.liferay.portal.kernel.job.JobScheduler;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.ServerDetector;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Time;
29  
30  import java.util.Date;
31  
32  import org.quartz.JobDetail;
33  import org.quartz.Scheduler;
34  import org.quartz.SimpleTrigger;
35  import org.quartz.Trigger;
36  
37  /**
38   * <a href="JobSchedulerImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class JobSchedulerImpl implements JobScheduler {
44  
45      public void schedule(IntervalJob intervalJob) {
46          if (intervalJob == null) {
47              return;
48          }
49  
50          try {
51              if (_scheduler.isShutdown()) {
52                  return;
53              }
54          }
55          catch (Exception e) {
56              _log.error(e, e);
57          }
58  
59          String jobName =
60              intervalJob.getClass().getName() + StringPool.AT +
61                  intervalJob.hashCode();
62  
63          JobClassUtil.put(jobName, (Class<IntervalJob>)intervalJob.getClass());
64  
65          Date startTime = null;
66  
67          try {
68              if (ServerDetector.getServerId().equals(ServerDetector.TOMCAT_ID)) {
69                  startTime = new Date(System.currentTimeMillis() + Time.MINUTE);
70              }
71          }
72          catch (RuntimeException re) {
73  
74              // ServerDetector will throw an exception when JobSchedulerImpl is
75              // initialized in a test environment
76  
77          }
78  
79          if (startTime == null) {
80              startTime = new Date(System.currentTimeMillis() + Time.MINUTE * 3);
81          }
82  
83          Date endTime = null;
84  
85          JobDetail jobDetail = new JobDetail(
86              jobName, Scheduler.DEFAULT_GROUP, JobWrapper.class);
87  
88          Trigger trigger = new SimpleTrigger(
89              jobName, Scheduler.DEFAULT_GROUP, startTime, endTime,
90              SimpleTrigger.REPEAT_INDEFINITELY, intervalJob.getInterval());
91  
92          try {
93              _scheduler.scheduleJob(jobDetail, trigger);
94          }
95          catch (Exception e) {
96              _log.error(e, e);
97          }
98      }
99  
100     public void setScheduler(Scheduler scheduler) {
101         _scheduler = scheduler;
102     }
103 
104     public void shutdown() {
105         try {
106             if (!_scheduler.isShutdown()) {
107                 _scheduler.shutdown();
108             }
109         }
110         catch (Exception e) {
111             _log.error(e, e);
112         }
113     }
114 
115     public void unschedule(IntervalJob intervalJob) {
116         if (intervalJob == null) {
117             return;
118         }
119 
120         try {
121             if (_scheduler.isShutdown()) {
122                 return;
123             }
124         }
125         catch (Exception e) {
126             _log.error(e, e);
127         }
128 
129         try {
130             String jobName =
131                 intervalJob.getClass().getName() + StringPool.AT +
132                     intervalJob.hashCode();
133 
134             _scheduler.unscheduleJob(jobName, Scheduler.DEFAULT_GROUP);
135         }
136         catch (Exception e) {
137             _log.error(e, e);
138         }
139     }
140 
141     private static Log _log = LogFactoryUtil.getLog(JobScheduler.class);
142 
143     private Scheduler _scheduler;
144 
145 }