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 org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.turbine.modules.ScheduledJobLoader;
25  
26  /***
27   * Wrapper for a <code>JobEntry</code> to actually perform the job's action.
28   *
29   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
30   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
31   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33   * @version $Id: WorkerThread.java 534527 2007-05-02 16:10:59Z tv $
34   */
35  public class WorkerThread
36          implements Runnable
37  {
38      /***
39       * The <code>JobEntry</code> to run.
40       */
41      private JobEntry je = null;
42  
43      /*** Logging */
44      private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
45  
46      /***
47       * Creates a new worker to run the specified <code>JobEntry</code>.
48       *
49       * @param je The <code>JobEntry</code> to create a worker for.
50       */
51      public WorkerThread(JobEntry je)
52      {
53          this.je = je;
54      }
55  
56      /***
57       * Run the job.
58       */
59      public void run()
60      {
61          if (je == null || je.isActive())
62          {
63              return;
64          }
65  
66          try
67          {
68              if (!je.isActive())
69              {
70                  je.setActive(true);
71                  logStateChange("started");
72                  ScheduledJobLoader.getInstance().exec(je, je.getTask());
73              }
74          }
75          catch (Exception e)
76          {
77              log.error("Error in WorkerThread for scheduled job #" +
78                      je.getPrimaryKey() + ", task: " + je.getTask(), e);
79          }
80          finally
81          {
82              if (je.isActive())
83              {
84                  je.setActive(false);
85                  logStateChange("completed");
86              }
87          }
88      }
89  
90      /***
91       * Macro to log <code>JobEntry</code> status information.
92       *
93       * @param state The new state of the <code>JobEntry</code>.
94       */
95      private final void logStateChange(String state)
96      {
97          log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state +
98                  ", task: " + je.getTask());
99      }
100 }