1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.queue; |
19 |
|
|
20 |
|
import org.apache.camel.Consumer; |
21 |
|
import org.apache.camel.Exchange; |
22 |
|
import org.apache.camel.Processor; |
23 |
|
import org.apache.camel.impl.ServiceSupport; |
24 |
|
|
25 |
|
import java.util.concurrent.TimeUnit; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
public class QueueEndpointConsumer<E extends Exchange> extends ServiceSupport implements Consumer<E>, Runnable { |
31 |
|
private QueueEndpoint<E> endpoint; |
32 |
|
private Processor processor; |
33 |
|
private Thread thread; |
34 |
|
|
35 |
2 |
public QueueEndpointConsumer(QueueEndpoint<E> endpoint, Processor processor) { |
36 |
2 |
this.endpoint = endpoint; |
37 |
2 |
this.processor = processor; |
38 |
2 |
} |
39 |
|
|
40 |
|
@Override |
41 |
|
public String toString() { |
42 |
0 |
return "QueueEndpointConsumer: " + endpoint.getEndpointUri(); |
43 |
|
} |
44 |
|
|
45 |
|
public void run() { |
46 |
6 |
while (!isStopping()) { |
47 |
|
E exchange; |
48 |
|
try { |
49 |
4 |
exchange = endpoint.getQueue().poll(1000, TimeUnit.MILLISECONDS); |
50 |
|
} |
51 |
0 |
catch (InterruptedException e) { |
52 |
0 |
break; |
53 |
4 |
} |
54 |
4 |
if (exchange != null && !isStopping()) { |
55 |
|
try { |
56 |
2 |
processor.process(exchange); |
57 |
|
} |
58 |
0 |
catch (Throwable e) { |
59 |
0 |
e.printStackTrace(); |
60 |
2 |
} |
61 |
|
} |
62 |
4 |
} |
63 |
2 |
} |
64 |
|
|
65 |
|
protected void doStart() throws Exception { |
66 |
2 |
thread = new Thread(this, endpoint.getEndpointUri()); |
67 |
2 |
thread.setDaemon(true); |
68 |
2 |
thread.start(); |
69 |
2 |
} |
70 |
|
|
71 |
|
protected void doStop() throws Exception { |
72 |
2 |
thread.join(); |
73 |
2 |
} |
74 |
|
} |