1 package org.oddjob.framework; 2 3 4 import org.apache.log4j.MDC; 5 import org.oddjob.Resetable; 6 import org.oddjob.Stateful; 7 import org.oddjob.images.IconHelper; 8 import org.oddjob.logging.Log4jArchiver; 9 import org.oddjob.state.JobState; 10 import org.oddjob.util.OddjobLockedException; 11 12 18 19 public abstract class SimpleJob extends BasePrimary 20 implements Runnable , Resetable, Stateful { 21 22 26 protected boolean independant() { 27 return true; 28 } 29 30 36 abstract protected int execute() throws Throwable ; 37 38 42 public final void run() throws OddjobLockedException { 43 lock.accquire("Job executing."); 44 String oldMDC = (String )MDC.get(Log4jArchiver.MDC); 45 try { 46 MDC.put(Log4jArchiver.MDC, getLogger()); 47 if (!stateHandler.requestJobStateExecuting()) { 48 logger().debug("Can't execute job [" + this + "] because state is [" 49 + stateHandler.getJobState() + "]"); 50 return; 51 } 52 iconHelper.changeIcon(IconHelper.EXECUTING); 53 54 if (!configure()) { 56 return; 57 } 58 logger().info("Executing job [" + this + "]"); 59 60 int result = -1; 61 try { 62 result = execute(); 63 } 64 catch (Throwable e) { 65 logger().warn("Job Exception in [" + this + "]", e); 66 setJobStateException(e); 67 return; 68 } 69 70 logger().info("Finished job [" + this + "], result " + result); 71 72 if (result == 0) { 73 setJobStateComplete(); 74 } 75 else { 76 setJobStateNotComplete(); 77 } 78 } 79 finally { 80 if (oldMDC != null) { 81 MDC.put(Log4jArchiver.MDC, oldMDC); 82 } 83 else { 84 MDC.remove(Log4jArchiver.MDC); 85 } 86 lock.release(); 87 } 88 } 89 90 95 public final void stop() { 96 if (destroyed) { 97 throw new IllegalStateException ("[" + this + "] destroyed"); 98 } 99 if (stateHandler.getJobState() != JobState.EXECUTING) { 100 return; 101 } 102 logger().debug("Thread [" + Thread.currentThread().getName() 103 + "] requested [" + getName() + "] stop."); 104 iconHelper.changeIcon(IconHelper.STOPPING); 105 try { 106 onStop(); 107 } catch (RuntimeException e) { 108 iconHelper.changeIcon(IconHelper.EXECUTING); 109 throw e; 110 } 111 stop = true; 112 synchronized (this) { 113 notifyAll(); 114 } 115 } 116 117 public void onStop() { } 118 119 122 public void softReset() { 123 lock.accquire("Soft reset in progress."); 124 try { 125 logger().debug("Thread [" + Thread.currentThread().getName() 126 + "] soft reset for [" + this + "]."); 127 if (canSoftReset()) { 128 setJobStateReady(); 129 logger().info("Reset job [" + this + "]"); 130 } 131 } 132 finally { 133 lock.release(); 134 } 135 } 136 137 140 public void hardReset() { 141 lock.accquire("Hard reset in progress."); 142 try { 143 logger().debug("Thread [" + Thread.currentThread().getName() 144 + "] hard reset for [" + this + "]."); 145 if (canHardReset()) { 146 setJobStateReady(); 147 logger().info("Reset job [" + this + "]"); 148 } 149 } 150 finally { 151 lock.release(); 152 } 153 } 154 155 } 156 | Popular Tags |