View Javadoc

1   package org.apache.turbine.services.schedule;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Date;
23  import java.util.concurrent.atomic.AtomicBoolean;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.turbine.modules.ScheduledJobLoader;
27  import org.apache.turbine.util.TurbineException;
28  import org.quartz.Job;
29  import org.quartz.JobBuilder;
30  import org.quartz.JobDetail;
31  import org.quartz.JobExecutionContext;
32  import org.quartz.JobExecutionException;
33  import org.quartz.Trigger;
34  
35  public class JobEntryQuartz implements JobEntry, Job
36  {
37      private int jobId;
38      private Trigger jobTrigger;
39      private JobDetail jobDetail;
40      private String task;
41      private boolean isnew = true;
42      private AtomicBoolean active = new AtomicBoolean(false);
43  
44      public static final String DEFAULT_JOB_GROUP_NAME = "TURBINE";
45  
46      /**
47       * Default constructor
48       */
49      public JobEntryQuartz()
50      {
51          super();
52      }
53  
54      /**
55       * Constructor
56       *
57       * @param jobTrigger Job time table
58       */
59      public JobEntryQuartz(Trigger jobTrigger)
60      {
61          this(jobTrigger, JobBuilder
62                  .newJob(JobEntryQuartz.class)
63                  .withIdentity(jobTrigger.getJobKey().getName(), DEFAULT_JOB_GROUP_NAME).build());
64      }
65  
66      /**
67       * Constructor
68       *
69       * @param jobTrigger Job time table
70       * @param jobDetail job details
71       */
72      public JobEntryQuartz(Trigger jobTrigger, JobDetail jobDetail)
73      {
74          this();
75          setTask(jobTrigger.getJobKey().getName());
76          this.jobTrigger = jobTrigger;
77          this.jobDetail = jobDetail;
78      }
79  
80      /**
81       * Return true, if the entry is not yet persisted
82       */
83      @Override
84      public boolean isNew()
85      {
86          boolean _isnew = isnew;
87          isnew = false;
88          return _isnew;
89      }
90  
91      /**
92       * Get the value of jobId.
93       *
94       * @return int
95       */
96      @Override
97      public int getJobId()
98      {
99          return jobId;
100     }
101 
102     /**
103      * Set the value of jobId.
104      *
105      * @param v new value
106      */
107     @Override
108     public void setJobId(int v)
109     {
110         this.jobId = v;
111     }
112 
113     /**
114      * Get the value of task.
115      *
116      * @return String
117      */
118     @Override
119     public String getTask()
120     {
121         return task;
122     }
123 
124     /**
125      * Set the value of task.
126      *
127      * @param v new value
128      */
129     @Override
130     public void setTask(String v)
131     {
132         this.task = v;
133     }
134 
135     /**
136      * @return the jobTrigger
137      */
138     public Trigger getJobTrigger()
139     {
140         return jobTrigger;
141     }
142 
143     /**
144      * @param jobTrigger the jobTrigger to set
145      */
146     public void setJobTrigger(Trigger jobTrigger)
147     {
148         this.jobTrigger = jobTrigger;
149     }
150 
151     /**
152      * @return the jobDetail
153      */
154     public JobDetail getJobDetail()
155     {
156         return jobDetail;
157     }
158 
159     /**
160      * @see java.lang.Comparable#compareTo(java.lang.Object)
161      */
162     @Override
163     public int compareTo(JobEntry o)
164     {
165         return jobTrigger.compareTo(((JobEntryQuartz)o).getJobTrigger());
166     }
167 
168     /**
169      * @see org.apache.turbine.services.schedule.JobEntry#setActive(boolean)
170      */
171     @Override
172     public void setActive(boolean isActive)
173     {
174         this.active.set(isActive);
175     }
176 
177     /**
178      * @see org.apache.turbine.services.schedule.JobEntry#isActive()
179      */
180     @Override
181     public boolean isActive()
182     {
183         return active.get();
184     }
185 
186     /**
187      * @see org.apache.turbine.services.schedule.JobEntry#getNextRuntime()
188      */
189     @Override
190     public long getNextRuntime()
191     {
192         return getNextRunDate().getTime();
193     }
194 
195     /**
196      * @see org.apache.turbine.services.schedule.JobEntry#getNextRunDate()
197      */
198     @Override
199     public Date getNextRunDate()
200     {
201         return jobTrigger.getNextFireTime();
202     }
203 
204     /**
205      * @see org.apache.turbine.services.schedule.JobEntry#getNextRunAsString()
206      */
207     @Override
208     public String getNextRunAsString()
209     {
210         return getNextRunDate().toString();
211     }
212 
213     /**
214      * @see org.apache.turbine.services.schedule.JobEntry#calcRunTime()
215      */
216     @Override
217     public void calcRunTime() throws TurbineException
218     {
219         // do nothing
220     }
221 
222     /**
223      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
224      */
225     @Override
226     public void execute(JobExecutionContext context) throws JobExecutionException
227     {
228         if (active.compareAndSet(false, true) == false)
229         {
230             return;
231         }
232 
233         try
234         {
235             String task = getTask();
236             if (StringUtils.isEmpty(task))
237             {
238                 // This happens when the job is configured in the Quartz configuration file
239                 task = context.getJobDetail().getKey().getName();
240             }
241             ScheduledJobLoader.getInstance().exec(this, task);
242         }
243         catch (Exception e)
244         {
245             throw new JobExecutionException("Error executing scheduled job #" +
246                     getJobId() + ", task: " + getTask(), e);
247         }
248         finally
249         {
250             active.compareAndSet(true, false);
251         }
252     }
253 }