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.commons.logging.Log; |
30 |
|
import org.apache.commons.logging.LogFactory; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
357 |
public class DirectEndpoint<E extends Exchange> extends DefaultEndpoint<E> { |
40 |
3 |
private static final Log LOG = LogFactory.getLog(DirectEndpoint.class); |
41 |
|
|
42 |
186 |
boolean allowMultipleConsumers = true; |
43 |
186 |
private final CopyOnWriteArrayList<DefaultConsumer<E>> consumers = new CopyOnWriteArrayList<DefaultConsumer<E>>(); |
44 |
|
|
45 |
|
public DirectEndpoint(String uri, DirectComponent<E> component) { |
46 |
186 |
super(uri, component); |
47 |
186 |
} |
48 |
|
|
49 |
|
public Producer createProducer() throws Exception { |
50 |
183 |
return new DefaultProducer(this) { |
51 |
183 |
public void process(Exchange exchange) throws Exception { |
52 |
582 |
DirectEndpoint.this.process(exchange); |
53 |
582 |
} |
54 |
|
}; |
55 |
|
} |
56 |
|
|
57 |
|
protected void process(Exchange exchange) throws Exception { |
58 |
582 |
if (consumers.isEmpty()) { |
59 |
3 |
LOG.warn("No consumers available on " + this + " for " + exchange); |
60 |
3 |
} else { |
61 |
579 |
for (DefaultConsumer<E> consumer : consumers) { |
62 |
579 |
consumer.getProcessor().process(exchange); |
63 |
579 |
} |
64 |
|
} |
65 |
582 |
} |
66 |
|
|
67 |
|
public Consumer<E> createConsumer(Processor processor) throws Exception { |
68 |
180 |
return new DefaultConsumer<E>(this, processor) { |
69 |
|
@Override |
70 |
|
public void start() throws Exception { |
71 |
180 |
if (!allowMultipleConsumers && !consumers.isEmpty()) { |
72 |
0 |
throw new IllegalStateException("Endpoint " + getEndpointUri() + " only allows 1 active consumer but you attempted to start a 2nd consumer."); |
73 |
|
} |
74 |
|
|
75 |
180 |
consumers.add(this); |
76 |
180 |
super.start(); |
77 |
180 |
} |
78 |
|
|
79 |
|
@Override |
80 |
180 |
public void stop() throws Exception { |
81 |
177 |
super.stop(); |
82 |
177 |
consumers.remove(this); |
83 |
177 |
} |
84 |
|
}; |
85 |
|
} |
86 |
|
|
87 |
|
public E createExchange() { |
88 |
|
|
89 |
|
|
90 |
546 |
return (E)new DefaultExchange(getContext()); |
91 |
|
} |
92 |
|
|
93 |
|
public boolean isAllowMultipleConsumers() { |
94 |
0 |
return allowMultipleConsumers; |
95 |
|
} |
96 |
|
|
97 |
|
public void setAllowMultipleConsumers(boolean allowMutlipleConsumers) { |
98 |
0 |
this.allowMultipleConsumers = allowMutlipleConsumers; |
99 |
0 |
} |
100 |
|
|
101 |
|
public boolean isSingleton() { |
102 |
186 |
return true; |
103 |
|
} |
104 |
|
|
105 |
|
} |