我如何专门发送到已开始的骆驼路线? [英] How to I exclusively send to started Camel routes?

查看:29
本文介绍了我如何专门发送到已开始的骆驼路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 不发布它们……

这篇关于我如何专门发送到已开始的骆驼路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆