KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > schedule > WorkerThread


1 package org.apache.turbine.services.schedule;
2
3 /*
4  * Copyright 2001-2004 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,v 1.6.2.2 2004/05/20 03:06:49 seade Exp $
31  */

32 public class WorkerThread
33         implements Runnable JavaDoc
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 JavaDoc 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 JavaDoc state)
93     {
94         log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state +
95                 ", task: " + je.getTask());
96     }
97 }
98
Popular Tags