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.Consumer; |
21 |
|
import org.apache.camel.Endpoint; |
22 |
|
import org.apache.camel.Exchange; |
23 |
|
import org.apache.camel.Processor; |
24 |
|
import org.apache.camel.PollingConsumer; |
25 |
|
import org.apache.camel.processor.Logger; |
26 |
|
import org.apache.camel.spi.ExceptionHandler; |
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
|
30 |
|
import java.util.concurrent.ArrayBlockingQueue; |
31 |
|
import java.util.concurrent.BlockingQueue; |
32 |
|
import java.util.concurrent.TimeUnit; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
public class DefaultPollingConsumer<E extends Exchange> extends PollingConsumerSupport<E> implements Processor { |
41 |
1 |
private static final transient Log log = LogFactory.getLog(DefaultPollingConsumer.class); |
42 |
|
private BlockingQueue<E> queue; |
43 |
1 |
private ExceptionHandler interuptedExceptionHandler = new LoggingExceptionHandler(new Logger(log)); |
44 |
|
private Consumer<E> consumer; |
45 |
|
|
46 |
|
public DefaultPollingConsumer(Endpoint<E> endpoint) { |
47 |
1 |
this(endpoint, new ArrayBlockingQueue<E>(1000)); |
48 |
1 |
} |
49 |
|
|
50 |
|
public DefaultPollingConsumer(Endpoint<E> endpoint, BlockingQueue<E> queue) { |
51 |
1 |
super(endpoint); |
52 |
1 |
this.queue = queue; |
53 |
1 |
} |
54 |
|
|
55 |
|
public E receiveNoWait() { |
56 |
0 |
return receive(0); |
57 |
|
} |
58 |
|
|
59 |
|
public E receive() { |
60 |
0 |
while (!isStopping() && !isStopped()) { |
61 |
|
try { |
62 |
0 |
return queue.take(); |
63 |
|
} |
64 |
0 |
catch (InterruptedException e) { |
65 |
0 |
handleInteruptedException(e); |
66 |
|
} |
67 |
0 |
} |
68 |
0 |
return null; |
69 |
|
} |
70 |
|
|
71 |
|
public E receive(long timeout) { |
72 |
|
try { |
73 |
6 |
return queue.poll(timeout, TimeUnit.MILLISECONDS); |
74 |
|
} |
75 |
0 |
catch (InterruptedException e) { |
76 |
0 |
handleInteruptedException(e); |
77 |
0 |
return null; |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
|
public void process(Exchange exchange) throws Exception { |
82 |
4 |
queue.offer((E) exchange); |
83 |
4 |
} |
84 |
|
|
85 |
|
public ExceptionHandler getInteruptedExceptionHandler() { |
86 |
0 |
return interuptedExceptionHandler; |
87 |
|
} |
88 |
|
|
89 |
|
public void setInteruptedExceptionHandler(ExceptionHandler interuptedExceptionHandler) { |
90 |
0 |
this.interuptedExceptionHandler = interuptedExceptionHandler; |
91 |
0 |
} |
92 |
|
|
93 |
|
protected void handleInteruptedException(InterruptedException e) { |
94 |
0 |
getInteruptedExceptionHandler().handleException(e); |
95 |
0 |
} |
96 |
|
|
97 |
|
protected void doStart() throws Exception { |
98 |
|
|
99 |
1 |
consumer = getEndpoint().createConsumer(this); |
100 |
1 |
consumer.start(); |
101 |
1 |
} |
102 |
|
|
103 |
|
protected void doStop() throws Exception { |
104 |
1 |
if (consumer != null) { |
105 |
|
try { |
106 |
1 |
consumer.stop(); |
107 |
|
} |
108 |
|
finally { |
109 |
1 |
consumer = null; |
110 |
1 |
} |
111 |
|
} |
112 |
1 |
} |
113 |
|
} |