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  import org.quartz.impl.StdSchedulerFactory;
41  
42  /**
43   * <a href="JobSchedulerImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class JobSchedulerImpl implements JobScheduler {
49  
50      public JobSchedulerImpl() {
51          StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
52  
53          try {
54              _scheduler = schedulerFactory.getScheduler();
55  
56              _scheduler.start();
57          }
58          catch (Exception e) {
59              _log.error(e);
60          }
61      }
62  
63      public void schedule(IntervalJob intervalJob) {
64          if (intervalJob == null) {
65              return;
66          }
67  
68          String jobName =
69              intervalJob.getClass().getName() + StringPool.AT +
70                  intervalJob.hashCode();
71  
72          JobClassUtil.put(jobName, (Class<IntervalJob>)intervalJob.getClass());
73  
74          Date startTime = null;
75  
76          if (ServerDetector.getServerId().equals(ServerDetector.TOMCAT_ID)) {
77              startTime = new Date(System.currentTimeMillis() + Time.MINUTE);
78          }
79          else {
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 shutdown() {
101         try {
102             if (!_scheduler.isShutdown()) {
103                 _scheduler.shutdown();
104             }
105         }
106         catch (Exception e) {
107             _log.error(e, e);
108         }
109     }
110 
111     public void unschedule(IntervalJob intervalJob) {
112         try {
113             if (intervalJob == null) {
114                 return;
115             }
116 
117             String jobName =
118                 intervalJob.getClass().getName() + StringPool.AT +
119                     intervalJob.hashCode();
120 
121             _scheduler.unscheduleJob(jobName, Scheduler.DEFAULT_GROUP);
122         }
123         catch (Exception e) {
124             _log.error(e, e);
125         }
126     }
127 
128     private static Log _log = LogFactory.getLog(JobScheduler.class);
129 
130     private Scheduler _scheduler;
131 
132 }