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      @Override
60      public void run()
61      {
62          if (je == null || je.isActive())
63          {
64              return;
65          }
66  
67          try
68          {
69              if (!je.isActive())
70              {
71                  je.setActive(true);
72                  logStateChange("started");
73                  ScheduledJobLoader.getInstance().exec(je, je.getTask());
74              }
75          }
76          catch (Exception e)
77          {
78              log.error("Error in WorkerThread for scheduled job #" +
79                      je.getJobId() + ", task: " + je.getTask(), e);
80          }
81          finally
82          {
83              if (je.isActive())
84              {
85                  je.setActive(false);
86                  logStateChange("completed");
87              }
88          }
89      }
90  
91      /**
92       * Macro to log <code>JobEntry</code> status information.
93       *
94       * @param state The new state of the <code>JobEntry</code>.
95       */
96      private final void logStateChange(String state)
97      {
98          log.debug("Scheduled job #" + je.getJobId() + ' ' + state +
99                  ", task: " + je.getTask());
100     }
101 }