Camel Java DSL - 基于标头内容的路由器或动态路由器中的 ENUM 路由

发布于 2024-11-26 10:32:55 字数 760 浏览 0 评论 0原文

我有一个 bean,它生成对象并使用 Camel 中的 ProducerTemplate 将它们发送到 SEDA 队列。

我发送一个正文和一个标头:

producerTemp.sendBodyAndHeader(document, "sourceSystem", sourceSys);

这里标头名称是 sourceSystem ,标头对象值是一个 ENUM (sourceSys),其中包含 document< /code> 包含许多不同属性的对象。

我想以并发方式从 SEDA 队列中提取消息,并根据 sourceSys 枚举的值将它们发送到不同的端点。

骆驼中使用此功能最有效的 EIP 是什么?有人有使用 Java DSL 的示例吗?我不确定如何测试 Enum 的值?

我想我会做这样的事情:

from("seda:a")
    .choice()
        .when(header("foo").isEqualTo(SourceSysEnum.SYSTEM1))
            .to("seda:b")
        .when(header("foo").isEqualTo(SourceSysEnum.SYSTEM2))
            .to("seda:c")
        .otherwise()
            .to("seda:d");

..?

I have a bean which produces objects and sends them to a SEDA queue using the ProducerTemplate in Camel.

I send a body and a header:

producerTemp.sendBodyAndHeader(document, "sourceSystem", sourceSys);

Here the header name is sourceSystem and the header object value is an ENUM (sourceSys) which contains the source of the document object containing a number of differnt attribs.

I want to pull messages in a concurrent fashion from the SEDA queue and send them to different endpoints depending on the value of the sourceSys enum.

What is the most efficient EIP in camel to use this and does anyone have an example using Java DSL, I'm not sure how I can test the value of the Enum?

I am thinking I do something like this:

from("seda:a")
    .choice()
        .when(header("foo").isEqualTo(SourceSysEnum.SYSTEM1))
            .to("seda:b")
        .when(header("foo").isEqualTo(SourceSysEnum.SYSTEM2))
            .to("seda:c")
        .otherwise()
            .to("seda:d");

..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-12-03 10:32:55

您可以使用收件人列表EIP
http://camel.apache.org/recipient-list.html

然后例如使用 java bean 来计算消息应该发送到的 uri。

from("seda:a")
  .recpientList().method(MyBean.class, "whereToGo");

并且在bean中您可以使用bean参数绑定。

这样就可以绑定了header如下:

public class MyBean {

   public String whereToGo(String body, @Header("foo") SourceSysEnum sys) {
      ...
   }
}

如果不需要消息体,那么您可以省略该参数。

You can use the recipient list EIP
http://camel.apache.org/recipient-list.html

And then for example use a java bean to compute the uri where the message should go.

from("seda:a")
  .recpientList().method(MyBean.class, "whereToGo");

And in the bean you can use bean parameter binding.

So you can bind the header as follows:

public class MyBean {

   public String whereToGo(String body, @Header("foo") SourceSysEnum sys) {
      ...
   }
}

If you dont need the message body, then you can omit that parameter.

浮世清欢 2024-12-03 10:32:55

您可以将ProcessorRouting Slip 结合使用,通过switch 语句来完成此操作。我不确定这会提高多少效率,除非你有大量的枚举值。但是,如果您将来需要添加更复杂的逻辑,它将为您提供更大的灵活性。

from("seda:a")
    .process(new SourceSysRoutingSlipProvider())
    .routingSlip(SourceSysRoutingSlipProvider.HEADER_NAME);


public class SourceSysRoutingSlipProvider : Processor {
  public static String HEADER_NAME="sourceSystemRoutes";

  public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();
    switch( in.getHeader("sourceSystem") ) {
      case SourceSysEnum.SYSTEM1:
         in.setHeader(HEADER_NAME, "seda:b");
         break;
      case SourceSysEnum.SYSTEM2:
         in.setHeader(HEADER_NAME, "seda:c");
         break;
      ...
      default:
         in.setHeader(HEADER_NAME, "seda:d");
         break;
    } 
  }
}

You could use a Processor combined with a Routing Slip to accomplish this using a switch statement. I'm not sure how much more efficient this will be unless you have a ton of enum values. It will, however, give you more flexibility should you need to add more complicated logic in the future.

from("seda:a")
    .process(new SourceSysRoutingSlipProvider())
    .routingSlip(SourceSysRoutingSlipProvider.HEADER_NAME);


public class SourceSysRoutingSlipProvider : Processor {
  public static String HEADER_NAME="sourceSystemRoutes";

  public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();
    switch( in.getHeader("sourceSystem") ) {
      case SourceSysEnum.SYSTEM1:
         in.setHeader(HEADER_NAME, "seda:b");
         break;
      case SourceSysEnum.SYSTEM2:
         in.setHeader(HEADER_NAME, "seda:c");
         break;
      ...
      default:
         in.setHeader(HEADER_NAME, "seda:d");
         break;
    } 
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文