View Javadoc

1   package org.apache.turbine.services.schedule;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.turbine.modules.ScheduledJobLoader;
22  
23  /***
24   * Wrapper for a <code>JobEntry</code> to actually perform the job's action.
25   *
26   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
27   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
28   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
29   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
30   * @version $Id: WorkerThread.java 264148 2005-08-29 14:21:04Z henning $
31   */
32  public class WorkerThread
33          implements Runnable
34  {
35      /***
36       * The <code>JobEntry</code> to run.
37       */
38      private JobEntry je = null;
39  
40      /*** Logging */
41      private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
42  
43      /***
44       * Creates a new worker to run the specified <code>JobEntry</code>.
45       *
46       * @param je The <code>JobEntry</code> to create a worker for.
47       */
48      public WorkerThread(JobEntry je)
49      {
50          this.je = je;
51      }
52  
53      /***
54       * Run the job.
55       */
56      public void run()
57      {
58          if (je == null || je.isActive())
59          {
60              return;
61          }
62  
63          try
64          {
65              if (!je.isActive())
66              {
67                  je.setActive(true);
68                  logStateChange("started");
69                  ScheduledJobLoader.getInstance().exec(je, je.getTask());
70              }
71          }
72          catch (Exception e)
73          {
74              log.error("Error in WorkerThread for scheduled job #" +
75                      je.getPrimaryKey() + ", task: " + je.getTask(), e);
76          }
77          finally
78          {
79              if (je.isActive())
80              {
81                  je.setActive(false);
82                  logStateChange("completed");
83              }
84          }
85      }
86  
87      /***
88       * Macro to log <code>JobEntry</code> status information.
89       *
90       * @param state The new state of the <code>JobEntry</code>.
91       */
92      private final void logStateChange(String state)
93      {
94          log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state +
95                  ", task: " + je.getTask());
96      }
97  }