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