【问题标题】:Restrict the null body from entering into a camel endpoint in xml限制 null 正文进入 xml 中的骆驼端点
【发布时间】:2015-10-17 05:19:16
【问题描述】:
<camel:route id="messageRoute">    
<camel:from ref="fromMessageQueue" />
<camel:processor ref="queueMessageProcessor" />
<camel:to ref="toMessageQueue" />
</camel:route>

在这段代码 sn-p 中,从 q 队列接收消息,然后在队列消息处理器中处理它,最后将其放入消息队列。在处理器中处理消息时,交换主体被设置为空。我需要防止带有空正文的交换进入消息队列。

【问题讨论】:

    标签: java jakarta-ee apache-camel integration


    【解决方案1】:

    尝试使用simple languagecontent based router

    <camel:route id="messageRoute">    
        <camel:from ref="fromMessageQueue" />
        <camel:processor ref="queueMessageProcessor" />
        <camel:choice>
            <camel:when>
                <camel:simple>${body} != null</camel:simple>
                <camel:to ref="toMessageQueue" />
            </camel:when>
        </camel:choice>
    </camel:route>
    

    【讨论】:

      【解决方案2】:

      使用交换模式 InOut 即

      <blockquote>
        <camel:route id="messageRoute">    
        <camel:from ref="fromMessageQueue" />
        <camel:processor ref="queueMessageProcessor" />
        **<setExchangePattern pattern="InOut"/>**
        <camel:to ref="toMessageQueue" />
        </camel:route>
      </blockquote>
      

      基本上,您是在告诉 Camel 在同一条消息中对其进行修改,然后将修改后的消息发送回(Out)。

      【讨论】:

      • 交换模式如何解决这个问题?如果在 out-exchange 的主体中设置了 null,那不会进入下一个端点吗?
      • 如果你在 out body 中设置 null 意味着你想在下一个端点传递那个值。你会更多地解释这个用例吗?
      • 在处理器中创建一个信封并将其设置到交换体中。然后将此交换传递给下一个端点,即消息队列。有一种情况是,创建的信封为空,因此由于交换为空主体而导致 NPE。需要防止与空正文交换进入消息队列。
      【解决方案3】:

      @Amit:这是避免 NPE 的方法: 1.根据正文值在您的骆驼流程中设置标题,例如 这里我是根据 In body 内容设置 header IsNull。

      public class QueueManagerProcessor implements Processor{
      
          @Override
          public void process(Exchange exchange) throws Exception {
      
              if(exchange.getIn().getBody()==null){
                  exchange.getOut().setHeader("IsNull", "true");
              }else{
                  exchange.getOut().setHeader("IsNull", "false");
              }
          }
      
      }
      
      1. 在您的 Route Builder 中,您可以使用选项和条件来应用基于内容的路由

               <xpath>$IsNull = 'false'</xpath>
                 <camel:to ref="toMessageQueue" />
        

      【讨论】:

      • 投票赞成你的答案,对我有用。但是@Husky227 的解决方案看起来更干净。
      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多