001 package 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 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 import 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 */ 035 public 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 public void run() 060 { 061 if (je == null || je.isActive()) 062 { 063 return; 064 } 065 066 try 067 { 068 if (!je.isActive()) 069 { 070 je.setActive(true); 071 logStateChange("started"); 072 ScheduledJobLoader.getInstance().exec(je, je.getTask()); 073 } 074 } 075 catch (Exception e) 076 { 077 log.error("Error in WorkerThread for scheduled job #" + 078 je.getPrimaryKey() + ", task: " + je.getTask(), e); 079 } 080 finally 081 { 082 if (je.isActive()) 083 { 084 je.setActive(false); 085 logStateChange("completed"); 086 } 087 } 088 } 089 090 /** 091 * Macro to log <code>JobEntry</code> status information. 092 * 093 * @param state The new state of the <code>JobEntry</code>. 094 */ 095 private final void logStateChange(String state) 096 { 097 log.debug("Scheduled job #" + je.getPrimaryKey() + ' ' + state + 098 ", task: " + je.getTask()); 099 } 100 }