001package org.apache.turbine.services.schedule;
002
003import org.apache.logging.log4j.LogManager;
004
005/*
006 * Licensed to the Apache Software Foundation (ASF) under one
007 * or more contributor license agreements.  See the NOTICE file
008 * distributed with this work for additional information
009 * regarding copyright ownership.  The ASF licenses this file
010 * to you under the Apache License, Version 2.0 (the
011 * "License"); you may not use this file except in compliance
012 * with the License.  You may obtain a copy of the License at
013 *
014 *   http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing,
017 * software distributed under the License is distributed on an
018 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019 * KIND, either express or implied.  See the License for the
020 * specific language governing permissions and limitations
021 * under the License.
022 */
023
024import org.apache.logging.log4j.Logger;
025import org.apache.turbine.modules.ScheduledJobLoader;
026
027/**
028 * Wrapper for a <code>JobEntry</code> to actually perform the job's action.
029 *
030 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
031 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
032 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
033 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
034 * @version $Id: WorkerThread.java 534527 2007-05-02 16:10:59Z tv $
035 */
036public class WorkerThread
037        implements Runnable
038{
039    /**
040     * The <code>JobEntry</code> to run.
041     */
042    private JobEntry je = null;
043
044    /** Logging */
045    private static final Logger log = LogManager.getLogger(ScheduleService.LOGGER_NAME);
046
047    /**
048     * Creates a new worker to run the specified <code>JobEntry</code>.
049     *
050     * @param je The <code>JobEntry</code> to create a worker for.
051     */
052    public WorkerThread(JobEntry je)
053    {
054        this.je = je;
055    }
056
057    /**
058     * Run the job.
059     */
060    @Override
061    public void run()
062    {
063        if (je == null || je.isActive())
064        {
065            return;
066        }
067
068        try
069        {
070            if (!je.isActive())
071            {
072                je.setActive(true);
073                logStateChange("started");
074                ScheduledJobLoader.getInstance().exec(je, je.getTask());
075            }
076        }
077        catch (Exception e)
078        {
079            log.error("Error in WorkerThread for scheduled job #{}, task: {}",
080                    Integer.valueOf(je.getJobId()), je.getTask(), e);
081        }
082        finally
083        {
084            if (je.isActive())
085            {
086                je.setActive(false);
087                logStateChange("completed");
088            }
089        }
090    }
091
092    /**
093     * Macro to log <code>JobEntry</code> status information.
094     *
095     * @param state The new state of the <code>JobEntry</code>.
096     */
097    private final void logStateChange(String state)
098    {
099        log.error("Scheduled job #{} {}, task: {}",
100                Integer.valueOf(je.getJobId()), state, je.getTask());
101    }
102}