1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.direct; |
18 |
|
|
19 |
|
import java.util.concurrent.CopyOnWriteArrayList; |
20 |
|
|
21 |
|
import org.apache.camel.Consumer; |
22 |
|
import org.apache.camel.Exchange; |
23 |
|
import org.apache.camel.Processor; |
24 |
|
import org.apache.camel.Producer; |
25 |
|
import org.apache.camel.impl.DefaultConsumer; |
26 |
|
import org.apache.camel.impl.DefaultEndpoint; |
27 |
|
import org.apache.camel.impl.DefaultExchange; |
28 |
|
import org.apache.camel.impl.DefaultProducer; |
29 |
|
import org.apache.camel.util.ProducerCache; |
30 |
|
import org.apache.commons.logging.Log; |
31 |
|
import org.apache.commons.logging.LogFactory; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
73 |
public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> { |
41 |
1 |
private static final Log log = LogFactory.getLog(DirectEndpoint.class); |
42 |
|
|
43 |
38 |
private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>(); |
44 |
|
|
45 |
38 |
boolean allowMultipleConsumers=true; |
46 |
|
|
47 |
|
public DirectEndpoint(String uri, DirectComponent<E> component) { |
48 |
38 |
super(uri, component); |
49 |
38 |
} |
50 |
|
|
51 |
|
public Producer createProducer() throws Exception { |
52 |
38 |
return new DefaultProducer(this) { |
53 |
38 |
public void process(Exchange exchange) throws Exception { |
54 |
48 |
DirectEndpoint.this.process(exchange); |
55 |
48 |
} |
56 |
|
}; |
57 |
|
} |
58 |
|
|
59 |
|
protected void process(Exchange exchange) throws Exception { |
60 |
48 |
if (consumers.isEmpty()) { |
61 |
1 |
log.warn("No consumers available on " + this + " for " + exchange); |
62 |
1 |
} |
63 |
|
else { |
64 |
47 |
for (DefaultConsumer<E> consumer : consumers) { |
65 |
47 |
consumer.getProcessor().process(exchange); |
66 |
47 |
} |
67 |
|
} |
68 |
48 |
} |
69 |
|
|
70 |
|
public Consumer<E> createConsumer(Processor processor) throws Exception { |
71 |
37 |
return new DefaultConsumer<E>(this, processor) { |
72 |
|
@Override |
73 |
|
public void start() throws Exception { |
74 |
37 |
if( !allowMultipleConsumers && !consumers.isEmpty() ) |
75 |
0 |
throw new IllegalStateException("Endpoint "+getEndpointUri()+" only allows 1 active consumer but you attempted to start a 2nd consumer."); |
76 |
|
|
77 |
37 |
consumers.add(this); |
78 |
37 |
super.start(); |
79 |
37 |
} |
80 |
|
|
81 |
|
@Override |
82 |
37 |
public void stop() throws Exception { |
83 |
36 |
super.stop(); |
84 |
36 |
consumers.remove(this); |
85 |
36 |
} |
86 |
|
}; |
87 |
|
} |
88 |
|
|
89 |
|
public E createExchange() { |
90 |
|
|
91 |
|
|
92 |
41 |
return (E) new DefaultExchange(getContext()); |
93 |
|
} |
94 |
|
|
95 |
|
public boolean isAllowMultipleConsumers() { |
96 |
0 |
return allowMultipleConsumers; |
97 |
|
} |
98 |
|
public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) { |
99 |
0 |
this.allowMultipleConsumers = allowMutlipleConsumers; |
100 |
0 |
} |
101 |
|
|
102 |
|
public boolean isSingleton() { |
103 |
38 |
return true; |
104 |
|
} |
105 |
|
|
106 |
|
} |