问题
public class MyRoute extends RouteBuilder {
@Override
public void configure() {
from("servlet://myservlet")
.multicast()
.parallelProcessing().recipientList(bean(this))
.end();
}
@RecipientList
public List<String> route(String body) {
return getContext().getRouteDefinitions().stream()
.filter(i -> i.getStatus(getContext()).isStarted() && i.getId().startsWith("FOO"))
.map(OptionalIdentifiedDefinition::getId)
.collect(toList());
}
}
When I debug, I see that getContext().getRouteDefinitions()
is empty, even though the routes are actually started. What am I doing wrong?
回答1:
RomanVottner provided a lot of insight by suggesting using the context from the exchange, and using ProducerTemplate. Here's what I ended up with:
from("servlet://my-endpoint")
.process(exchange -> {
ProducerTemplate template = exchange.getContext().createProducerTemplate();
exchange.getContext().getRouteDefinitions().stream()
.filter(routeDef ->
routeDef.getStatus(getContext()).isStarted() && i.getId().startsWith("FOO"))
.map(OptionalIdentifiedDefinition::getId)
.forEach(endpoint ->
template.asyncSendBody(endpoint, exchange.getIn().getBody()));
});
EDIT: Warning! After using asyncSendBody in production, the machine went out of PIDs pretty fast. I'll have to figure out why Camel isn't releasing them...
来源:https://stackoverflow.com/questions/41633061/how-to-i-exclusively-send-to-started-camel-routes