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