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