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