1 17 package org.apache.servicemix.beanflow; 18 19 import java.util.Timer ; 20 import java.util.TimerTask ; 21 22 28 public class TimeoutActivity extends AbstractActivity { 29 30 private State<Boolean > timedOut = new DefaultState<Boolean >(Boolean.FALSE); 31 private TimerTask timeoutTask; 32 33 36 public void onTimedOut() { 37 if (!isStopped()) { 39 timedOut.set(Boolean.TRUE); 40 } 41 } 42 43 public void run() { 44 if (!isStopped()) { 45 if (timedOut.get().booleanValue()) { 46 fail("Timed out"); 47 } 48 else { 49 onValidStateChange(); 50 } 51 } 52 } 53 54 57 public boolean isTimedOut() { 58 return timedOut.get(); 59 } 60 61 public void startWithTimeout(Timer timer, long timeout) { 62 scheduleTimeout(timer, timeout); 63 start(); 64 } 65 66 69 public void scheduleTimeout(Timer timer, long timeout) { 70 if (timeout > 0) { 71 timer.schedule(getTimeoutTask(), timeout); 72 } 73 } 74 75 78 public TimerTask getTimeoutTask() { 79 if (timeoutTask == null) { 80 timeoutTask = new TimerTask () { 81 public void run() { 82 onTimedOut(); 83 } 84 }; 85 } 86 return timeoutTask; 87 } 88 89 93 protected void onValidStateChange() { 94 } 95 } 96 | Popular Tags |