How to I exclusively send to started Camel routes?

落爺英雄遲暮 提交于 2020-01-06 18:13:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!