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