| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.processor; |
| 18 |
|
|
| 19 |
|
import java.util.concurrent.CountDownLatch; |
| 20 |
|
import java.util.concurrent.TimeUnit; |
| 21 |
|
|
| 22 |
|
import org.apache.camel.AlreadyStoppedException; |
| 23 |
|
import org.apache.camel.Exchange; |
| 24 |
|
import org.apache.camel.Processor; |
| 25 |
|
import org.apache.commons.logging.Log; |
| 26 |
|
import org.apache.commons.logging.LogFactory; |
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
public abstract class DelayProcessorSupport extends DelegateProcessor { |
| 35 |
3 |
private static final transient Log LOG = LogFactory.getLog(Delayer.class); |
| 36 |
6 |
private CountDownLatch stoppedLatch = new CountDownLatch(1); |
| 37 |
6 |
private boolean fastStop = true; |
| 38 |
|
|
| 39 |
|
public DelayProcessorSupport(Processor processor) { |
| 40 |
6 |
super(processor); |
| 41 |
6 |
} |
| 42 |
|
|
| 43 |
|
public void process(Exchange exchange) throws Exception { |
| 44 |
12 |
delay(exchange); |
| 45 |
12 |
super.process(exchange); |
| 46 |
12 |
} |
| 47 |
|
|
| 48 |
|
public boolean isFastStop() { |
| 49 |
6 |
return fastStop; |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
public void setFastStop(boolean fastStop) { |
| 59 |
0 |
this.fastStop = fastStop; |
| 60 |
0 |
} |
| 61 |
|
|
| 62 |
|
protected void doStop() throws Exception { |
| 63 |
6 |
stoppedLatch.countDown(); |
| 64 |
6 |
super.doStop(); |
| 65 |
6 |
} |
| 66 |
|
|
| 67 |
|
protected abstract void delay(Exchange exchange) throws Exception; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
protected void waitUntil(long time, Exchange exchange) throws Exception { |
| 76 |
|
while (true) { |
| 77 |
6 |
long delay = time - currentSystemTime(); |
| 78 |
6 |
if (delay < 0) { |
| 79 |
3 |
return; |
| 80 |
|
} else { |
| 81 |
3 |
if (isFastStop() && (isStopped() || isStopping())) { |
| 82 |
0 |
throw new AlreadyStoppedException(); |
| 83 |
|
} |
| 84 |
|
try { |
| 85 |
3 |
sleep(delay); |
| 86 |
0 |
} catch (InterruptedException e) { |
| 87 |
0 |
handleSleepInteruptedException(e); |
| 88 |
3 |
} |
| 89 |
|
} |
| 90 |
3 |
} |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
protected void sleep(long delay) throws InterruptedException { |
| 94 |
3 |
if (LOG.isDebugEnabled()) { |
| 95 |
0 |
LOG.debug("Sleeping for: " + delay + " millis"); |
| 96 |
|
} |
| 97 |
3 |
if (isFastStop()) { |
| 98 |
3 |
stoppedLatch.await(delay, TimeUnit.MILLISECONDS); |
| 99 |
3 |
} else { |
| 100 |
0 |
Thread.sleep(delay); |
| 101 |
|
} |
| 102 |
3 |
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
protected void handleSleepInteruptedException(InterruptedException e) { |
| 109 |
0 |
LOG.debug("Sleep interupted: " + e, e); |
| 110 |
0 |
} |
| 111 |
|
|
| 112 |
|
protected long currentSystemTime() { |
| 113 |
15 |
return System.currentTimeMillis(); |
| 114 |
|
} |
| 115 |
|
} |