【问题标题】:Grouping or batching Camel processors together inside a multicaster在多播器内将 Camel 处理器分组或批处理
【发布时间】:2019-03-27 09:04:22
【问题描述】:

Java 8 和 Apache Camel 2.19.5 在这里。我想定义一个完成以下任务的路由(使用 Spring XML DSL):

  1. 使用来自 ActiveMQ 队列的消息
  2. 将 XML 消息从 String 反序列化为 Fizzbuzz 实例
  3. Fizzbuzz 发送到FoobarGenerator 处理器
  4. FoobarGenerator 处理器在交易所生成Foobar
  5. Foobar 消息的副本随后同时传递给两个下游处理器:(a) Configurator 处理器和 (b) Analyzer 处理器 6a。 Configurator 处理 Foobar 并将一些结果存储在数据库中 6b。 Analyzer 处理 Foobar 然后路由将该实例发送到反序列化器(返回到字符串),然后将该字符串发送到另一个 AMQ 队列

这是我目前所拥有的:

<route id="fizzbuzz_processor">
  <!-- Step 1: Consume Fizzbuzz XML from AMQ -->
  <from uri="activemq:fizzbuzzes"/>

  <!-- Step 2: Deserialize into Fizzbuzz POJO instance, using XStream -->
  <unmarshall ref="xs"/>

  <!--
    Step 3 + 4: Send to FoobarGenerator processor;
    output is a 'Foobar' POJO instance
  -->
  <to uri="bean:foobarGenerator"/>

  <!-- Step 5: Send Foobar to two places at the same time -->
  <multicast>
    <!-- Step 6a: Foobar goes to Configurator processor -->
    <to uri="bean:configurator"/>

    <!-- Step 6b: Foobar also goes to Analyzer processor -->
    <to uri="bean:analyzer"/>

    <!--
      AFTER Analyzer, Foobar gets serialized into XML and sent
      to another queue
    -->
    <marshall ref="xs"/>
    <to uri="activemq:foobars"/>
  </multicast>
</route>

这个设置的问题在于,据我所知,multicasters 只能使用它们将向其发送消息的顶级端点来定义。所以按照我上面配置的方式,多播器将把消息发送到:(a) bean:configurator、(b) bean:analyzer、(c) XStream/marshaller 和 (d) activemq:fizzbuzzes 在同时,而不是同时发送到 bean:configurator 和 bean:analyzer,然后在 bean:analyzer 之后,编组并发送到 AMQ。

我怎样才能重新配置这条路线来做我想做的事?

【问题讨论】:

    标签: java apache-camel


    【解决方案1】:

    原来你可以:

    <route id="fizzbuzz_processor">
      <!-- Step 1: Consume Fizzbuzz XML from AMQ -->
      <from uri="activemq:fizzbuzzes"/>
    
      <!-- Step 2: Deserialize into Fizzbuzz POJO instance, using XStream -->
      <unmarshall ref="xs"/>
    
      <!--
        Step 3 + 4: Send to FoobarGenerator processor;
        output is a 'Foobar' POJO instance
      -->
      <to uri="bean:foobarGenerator"/>
    
      <!-- Step 5: Send Foobar to two places at the same time -->
      <multicast parallelProcessing="true">
        <!-- Step 6a: Foobar goes to Configurator processor -->
        <to uri="bean:configurator"/>
    
        <!-- Step 6b: Foobar also goes to Analyzer processor -->
        <to uri="direct:someOtherRoute"/>
      </multicast>
    </route>
    

    direct:someOtherRoute 转到另一条路线,该路线实现了我描述的 6b 之后的所有内容,并且 boom 你已经同时多播到两个“地方”。

    【讨论】:

      【解决方案2】:

      为什么要使用多播 EIP?似乎您可以通过完全删除多播器来获得相同的结果,例如:

      <route id="fizzbuzz_processor">
        <!-- Step 1: Consume Fizzbuzz XML from AMQ -->
        <from uri="activemq:fizzbuzzes"/>
        <!-- Step 2: Deserialize into Fizzbuzz POJO instance, using XStream -->
        <unmarshall ref="xs"/>
        <!--
          Step 3 + 4: Send to FoobarGenerator processor;
          output is a 'Foobar' POJO instance
        -->
        <to uri="bean:foobarGenerator"/>
      
        <!-- Step 6a: Foobar goes to Configurator processor -->
        <to uri="bean:configurator"/>
      
        <!-- Step 6b: Foobar also goes to Analyzer processor -->
        <to uri="bean:analyzer"/>
        <marshall ref="xs"/>
        <to uri="activemq:foobars"/>
      </route>
      

      或者通过将 marshal/activemq 部分留在多播器之外。 Camel 应该完成多播器,然后继续执行 marshal/to。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 2020-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多