| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.event; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Exchange; |
| 20 |
|
import org.apache.camel.Processor; |
| 21 |
|
import org.apache.camel.Producer; |
| 22 |
|
import org.apache.camel.RuntimeCamelException; |
| 23 |
|
import org.apache.camel.impl.DefaultEndpoint; |
| 24 |
|
import org.apache.camel.impl.DefaultExchange; |
| 25 |
|
import org.apache.camel.impl.DefaultProducer; |
| 26 |
|
import org.apache.camel.processor.loadbalancer.LoadBalancer; |
| 27 |
|
import org.apache.camel.processor.loadbalancer.TopicLoadBalancer; |
| 28 |
|
|
| 29 |
|
import org.springframework.context.ApplicationContext; |
| 30 |
|
import org.springframework.context.ApplicationEvent; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
1 |
public class EventEndpoint extends DefaultEndpoint<Exchange> { |
| 39 |
|
private final EventComponent component; |
| 40 |
|
private LoadBalancer loadBalancer; |
| 41 |
|
|
| 42 |
|
public EventEndpoint(String endpointUri, EventComponent component) { |
| 43 |
56 |
super(endpointUri, component); |
| 44 |
56 |
this.component = component; |
| 45 |
56 |
} |
| 46 |
|
|
| 47 |
|
@Override |
| 48 |
|
public EventComponent getComponent() { |
| 49 |
1 |
return component; |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
public ApplicationContext getApplicationContext() { |
| 53 |
1 |
return getComponent().getApplicationContext(); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
public boolean isSingleton() { |
| 57 |
59 |
return true; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
public Exchange createExchange() { |
| 61 |
83 |
return new DefaultExchange(getContext()); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
public Producer<Exchange> createProducer() throws Exception { |
| 65 |
1 |
return new DefaultProducer<Exchange>(this) { |
| 66 |
1 |
public void process(Exchange exchange) throws Exception { |
| 67 |
1 |
ApplicationEvent event = toApplicationEvent(exchange); |
| 68 |
1 |
getApplicationContext().publishEvent(event); |
| 69 |
1 |
} |
| 70 |
|
}; |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
public EventConsumer createConsumer(Processor processor) throws Exception { |
| 74 |
1 |
return new EventConsumer(this, processor); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
public void onApplicationEvent(ApplicationEvent event) { |
| 78 |
82 |
Exchange exchange = createExchange(); |
| 79 |
82 |
exchange.getIn().setBody(event); |
| 80 |
|
try { |
| 81 |
82 |
getLoadBalancer().process(exchange); |
| 82 |
0 |
} catch (Exception e) { |
| 83 |
0 |
throw new RuntimeCamelException(e); |
| 84 |
82 |
} |
| 85 |
82 |
} |
| 86 |
|
|
| 87 |
|
public LoadBalancer getLoadBalancer() { |
| 88 |
84 |
if (loadBalancer == null) { |
| 89 |
53 |
loadBalancer = createLoadBalancer(); |
| 90 |
|
} |
| 91 |
84 |
return loadBalancer; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
public void setLoadBalancer(LoadBalancer loadBalancer) { |
| 95 |
0 |
this.loadBalancer = loadBalancer; |
| 96 |
0 |
} |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
public synchronized void consumerStarted(EventConsumer consumer) { |
| 101 |
1 |
getLoadBalancer().addProcessor(consumer.getProcessor()); |
| 102 |
1 |
} |
| 103 |
|
|
| 104 |
|
public synchronized void consumerStopped(EventConsumer consumer) { |
| 105 |
1 |
getLoadBalancer().removeProcessor(consumer.getProcessor()); |
| 106 |
1 |
} |
| 107 |
|
|
| 108 |
|
protected LoadBalancer createLoadBalancer() { |
| 109 |
53 |
return new TopicLoadBalancer(); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
protected ApplicationEvent toApplicationEvent(Exchange exchange) { |
| 113 |
1 |
ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class); |
| 114 |
1 |
if (event == null) { |
| 115 |
1 |
event = new CamelEvent(this, exchange); |
| 116 |
|
} |
| 117 |
1 |
return event; |
| 118 |
|
} |
| 119 |
|
} |