我如何专门发送到已开始的骆驼路线?

2022-01-19 00:00:00 java apache-camel
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());
  }
}

当我调试时,我看到 getContext().getRouteDefinitions() 是空的,即使路由实际上已经启动.我做错了什么?

When I debug, I see that getContext().getRouteDefinitions() is empty, even though the routes are actually started. What am I doing wrong?

推荐答案

RomanVottner 通过建议使用交换中的上下文和使用 生产者模板.这是我最终得到的结果:

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()));
  });

警告!在生产中使用 asyncSendBody 后,机器很快就用完了 PID.我得弄清楚为什么 Camel 不发布它们……

相关文章