1 package org.oddjob.samples; 2 3 import org.oddjob.Stoppable; 4 import org.oddjob.framework.SimpleJob; 5 6 7 public class RandomTask extends SimpleJob 8 implements Stoppable { 9 10 private long delay; 11 12 public long getDelay() { 13 14 return delay; 15 } 16 17 public void setDelay(long delay) { 18 19 this.delay = delay; 20 } 21 22 23 public int execute() throws Exception { 24 25 if (getDelay() == 0) { 26 setDelay(new Double (Math.random() * 10000).longValue()); 27 } 28 synchronized (this) { 29 this.wait(delay); 30 } 31 32 if (Math.random() > 0.9) { 33 34 throw new Exception ("Random Exception"); 35 } 36 37 if (Math.random() > 0.5 ) { 38 39 return 1; 40 } 41 else { 42 43 return 0; 44 } 45 } 46 47 public void onStop() { 48 synchronized (this) { 49 notifyAll(); 50 } 51 } 52 } 53 | Popular Tags |