| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.impl; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.Endpoint; |
| 21 |
|
import org.apache.camel.Exchange; |
| 22 |
|
import org.apache.camel.Processor; |
| 23 |
|
import org.apache.commons.logging.Log; |
| 24 |
|
import org.apache.commons.logging.LogFactory; |
| 25 |
|
|
| 26 |
|
import java.util.concurrent.ScheduledExecutorService; |
| 27 |
|
import java.util.concurrent.ScheduledFuture; |
| 28 |
|
import java.util.concurrent.TimeUnit; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
public abstract class ScheduledPollConsumer<E extends Exchange> extends DefaultConsumer<E> implements Runnable { |
| 36 |
1 |
private static final transient Log log = LogFactory.getLog(ScheduledPollConsumer.class); |
| 37 |
|
|
| 38 |
|
private final ScheduledExecutorService executor; |
| 39 |
1 |
private long initialDelay = 1000; |
| 40 |
1 |
private long delay = 500; |
| 41 |
1 |
private TimeUnit timeUnit = TimeUnit.MILLISECONDS; |
| 42 |
|
private boolean useFixedDelay; |
| 43 |
|
private ScheduledFuture<?> future; |
| 44 |
|
|
| 45 |
|
public ScheduledPollConsumer(DefaultEndpoint<E> endpoint, Processor processor) { |
| 46 |
1 |
this(endpoint, processor, endpoint.getExecutorService()); |
| 47 |
1 |
} |
| 48 |
|
|
| 49 |
|
public ScheduledPollConsumer(Endpoint<E> endpoint, Processor processor, ScheduledExecutorService executor) { |
| 50 |
1 |
super(endpoint, processor); |
| 51 |
1 |
this.executor = executor; |
| 52 |
1 |
if (executor == null) { |
| 53 |
0 |
throw new IllegalArgumentException("A non null ScheduledExecutorService must be provided."); |
| 54 |
|
} |
| 55 |
1 |
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
public void run() { |
| 61 |
1 |
log.debug("Starting to poll"); |
| 62 |
|
try { |
| 63 |
1 |
poll(); |
| 64 |
|
} |
| 65 |
0 |
catch (Exception e) { |
| 66 |
0 |
log.warn("Caught: " + e, e); |
| 67 |
1 |
} |
| 68 |
1 |
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
public long getInitialDelay() { |
| 73 |
1 |
return initialDelay; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
public void setInitialDelay(long initialDelay) { |
| 77 |
0 |
this.initialDelay = initialDelay; |
| 78 |
0 |
} |
| 79 |
|
|
| 80 |
|
public long getDelay() { |
| 81 |
1 |
return delay; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
public void setDelay(long delay) { |
| 85 |
0 |
this.delay = delay; |
| 86 |
0 |
} |
| 87 |
|
|
| 88 |
|
public TimeUnit getTimeUnit() { |
| 89 |
1 |
return timeUnit; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
public void setTimeUnit(TimeUnit timeUnit) { |
| 93 |
0 |
this.timeUnit = timeUnit; |
| 94 |
0 |
} |
| 95 |
|
|
| 96 |
|
public boolean isUseFixedDelay() { |
| 97 |
1 |
return useFixedDelay; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
public void setUseFixedDelay(boolean useFixedDelay) { |
| 101 |
0 |
this.useFixedDelay = useFixedDelay; |
| 102 |
0 |
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
protected abstract void poll() throws Exception; |
| 113 |
|
|
| 114 |
|
@Override |
| 115 |
|
protected void doStart() throws Exception { |
| 116 |
1 |
super.doStart(); |
| 117 |
1 |
if (isUseFixedDelay()) { |
| 118 |
0 |
future = executor.scheduleWithFixedDelay(this, getInitialDelay(), getDelay(), getTimeUnit()); |
| 119 |
0 |
} |
| 120 |
|
else { |
| 121 |
1 |
future = executor.scheduleAtFixedRate(this, getInitialDelay(), getDelay(), getTimeUnit()); |
| 122 |
|
} |
| 123 |
1 |
} |
| 124 |
|
|
| 125 |
|
@Override |
| 126 |
|
protected void doStop() throws Exception { |
| 127 |
1 |
if (future != null) { |
| 128 |
1 |
future.cancel(false); |
| 129 |
|
} |
| 130 |
1 |
super.doStop(); |
| 131 |
1 |
} |
| 132 |
|
} |