Camel输入/输出消息和相关ID混淆

时间:2017-12-03 10:52:17

标签: apache-camel jms activemq integration messaging

我很困惑Camel在使用邮件队列时如何自动形成邮件和相关标识符。

例如,我有以下路线的两项服务

<route id="cxfToJMSRoute">

        <from uri="cxf:bean:endpoint" />
        <convertBodyTo type="String" />

        <log message="..." />
        <log message="In message: ${in.body}" />
        <log message="In JMSMessageID: ${in.header.JMSMessageID}" />
        <log message="In JMSCorrelationID: ${in.header.JMSCorrelationID}" />

        <log message="Out message: ${out.body}" />
        <log message="Out JMSMessageID: ${out.header.JMSMessageID}" />
        <log message="Out JMSCorrelationID: ${out.header.JMSCorrelationID}" />

        <log message="Invoking service A by sending InOut message to api queue" />
        <to uri="activemq:queue:api?replyTo=result&amp;replyToType=Exclusive"
            pattern="InOut" />

        <log message="..." />
        <log message="Got message from Service A" />
        <log message="In message: ${in.body}" />
        <log message="In JMSMessageID: ${in.header.JMSMessageID}" />
        <log message="In JMSCorrelationID: ${in.header.JMSCorrelationID}" />

        <log message="Out message: ${out.body}" />
        <log message="Out JMSMessageID: ${out.header.JMSMessageID}" />
        <log message="Out JMSCorrelationID: ${out.header.JMSCorrelationID}" />

        <!-- Correlation ID? -->
        <pollEnrich>
            <constant>activemq:queue:reply</constant>
        </pollEnrich>

        <log message="..." />
        <log message="Got message from Service B" />
        <log message="In message: ${in.body}" />
        <log message="In JMSMessageID: ${in.header.JMSMessageID}" />
        <log message="In JMSCorrelationID: ${in.header.JMSCorrelationID}" />

        <log message="Out message: ${out.body}" />
        <log message="Out JMSMessageID: ${out.header.JMSMessageID}" />
        <log message="Out JMSCorrelationID: ${out.header.JMSCorrelationID}" />

        <transform>
            <simple>${in.body}</simple>
        </transform>

        <log message="After transform:" />
        <log message="In message: ${in.body}" />
        <log message="In JMSMessageID: ${in.header.JMSMessageID}" />
        <log message="In JMSCorrelationID: ${in.header.JMSCorrelationID}" />

        <log message="Out message: ${out.body}" />
        <log message="Out JMSMessageID: ${out.header.JMSMessageID}" />
        <log message="Out JMSCorrelationID: ${out.header.JMSCorrelationID}" />
        <log message="..." />
    </route>

-

<route id="apiToServiceARoute">
      <from uri="activemq:queue:api"/>

      <log message="..." />
      <log message="In message: ${in.body}" />
      <log message="In JMSMessageID: ${in.header.JMSMessageID}" />
      <log message="In JMSCorrelationID: ${in.header.JMSCorrelationID}" />

      <log message="Out message: ${out.body}" />
      <log message="Out JMSMessageID: ${out.header.JMSMessageID}" />
      <log message="Out JMSCorrelationID: ${out.header.JMSCorrelationID}" />

      <log message="Mocking service B by sending InOnly message to reply queue" />
      <to uri="activemq:queue:reply?exchangePattern=InOnly" pattern="InOnly" />

      <transform>
        <simple>Delegated</simple>
      </transform>

      <log message="..." />
      <log message="After transform:" />
      <log message="In message: ${in.body}" />
      <log message="In JMSMessageID: ${in.header.JMSMessageID}" />
      <log message="In JMSCorrelationID: ${in.header.JMSCorrelationID}" />

      <log message="Out message: ${out.body}" />
      <log message="Out JMSMessageID: ${out.header.JMSMessageID}" />
      <log message="Out JMSCorrelationID: ${out.header.JMSCorrelationID}" />
      <log message="..." />
  </route>

以下是根据日志消息的观察结果

  1. OUT消息始终为空。 Camel默认情况下是否总是使用IN,直到OUT被某个处理器创建为异常?
  2. JMSMessageIDJMSCorrelationID似乎没有任何联系。 Camel 使用JMSCorrelationID来关联消息吗?
  3. Camel设法在将JMSCorrelationID消息发送到请求源自的另一个队列(回复)时保留InOnly。这是可取的,但它实际上是如何做到的?保留JMSCorrelationID
  4. 具体规则是什么?

    以下是两个示例路线的实际日志

    2017-12-03 12:16:06.197  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : ...
    2017-12-03 12:16:06.197  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : In message: testi2
    2017-12-03 12:16:06.198  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : In JMSMessageID:
    2017-12-03 12:16:06.198  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : In JMSCorrelationID:
    2017-12-03 12:16:06.198  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : Out message:
    2017-12-03 12:16:06.198  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : Out JMSMessageID:
    2017-12-03 12:16:06.198  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : Out JMSCorrelationID:
    2017-12-03 12:16:06.198  INFO 13780 --- [qtp335580595-36] cxfToJMSRoute                            : Invoking service A by sending InOut message to api queue
    2017-12-03 12:16:06.569  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : ...
    2017-12-03 12:16:06.570  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Got message from Service A
    2017-12-03 12:16:06.570  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In message: Delegated
    2017-12-03 12:16:06.582  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In JMSMessageID: ID:DESKTOP-LI5P50P-54036-1512296151920-1:1:1:1:1
    2017-12-03 12:16:06.582  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In JMSCorrelationID: Camel-ID-DESKTOP-LI5P50P-1512296077166-0-2
    2017-12-03 12:16:06.582  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out message:
    2017-12-03 12:16:06.582  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out JMSMessageID:
    2017-12-03 12:16:06.583  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out JMSCorrelationID:
    2017-12-03 12:16:06.596  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : ...
    2017-12-03 12:16:06.596  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Got message from Service B
    2017-12-03 12:16:06.596  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In message: testi2
    2017-12-03 12:16:06.597  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In JMSMessageID: ID:DESKTOP-LI5P50P-54036-1512296151920-3:1:1:1:1
    2017-12-03 12:16:06.597  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In JMSCorrelationID: Camel-ID-DESKTOP-LI5P50P-1512296077166-0-2
    2017-12-03 12:16:06.597  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out message:
    2017-12-03 12:16:06.597  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out JMSMessageID:
    2017-12-03 12:16:06.597  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out JMSCorrelationID:
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : After transform:
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In message: testi2
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In JMSMessageID: ID:DESKTOP-LI5P50P-54036-1512296151920-3:1:1:1:1
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : In JMSCorrelationID: Camel-ID-DESKTOP-LI5P50P-1512296077166-0-2
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out message:
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out JMSMessageID:
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : Out JMSCorrelationID:
    2017-12-03 12:16:06.598  INFO 13780 --- [Manager[result]] cxfToJMSRoute                            : ...
    

    -

    2017-12-03 12:16:06.528  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : ...
    2017-12-03 12:16:06.531  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : In message: testi2
    2017-12-03 12:16:06.534  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : In JMSMessageID: ID:DESKTOP-LI5P50P-54046-1512296166309-1:1:2:1:1
    2017-12-03 12:16:06.534  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : In JMSCorrelationID: Camel-ID-DESKTOP-LI5P50P-1512296077166-0-2
    2017-12-03 12:16:06.534  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Out message:
    2017-12-03 12:16:06.535  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Out JMSMessageID:
    2017-12-03 12:16:06.535  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Out JMSCorrelationID:
    2017-12-03 12:16:06.535  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Mocking service B by sending InOnly message to reply queue
    2017-12-03 12:16:06.558  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : ...
    2017-12-03 12:16:06.558  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : After transform:
    2017-12-03 12:16:06.558  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : In message: Delegated
    2017-12-03 12:16:06.558  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : In JMSMessageID: ID:DESKTOP-LI5P50P-54046-1512296166309-1:1:2:1:1
    2017-12-03 12:16:06.559  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : In JMSCorrelationID: Camel-ID-DESKTOP-LI5P50P-1512296077166-0-2
    2017-12-03 12:16:06.559  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Out message:
    2017-12-03 12:16:06.559  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Out JMSMessageID:
    2017-12-03 12:16:06.559  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : Out JMSCorrelationID:
    2017-12-03 12:16:06.559  INFO 16972 --- [msConsumer[api]] apiToServiceARoute                       : ...
    

1 个答案:

答案 0 :(得分:4)

  
      
  1. OUT消息始终为空。默认情况下,Camel是否始终使用IN,直到某些处理器明确创建OUT为止?
  2.   

是。克隆整个org.apache.camel.Message非常昂贵。在第一次调用OUT

期间,会根据需要创建Exchange#getOut

请参阅Apache Camel: Message exchange patterns and the Exchange object

  
      
  1. JMSMessageIDJMSCorrelationID似乎没有任何联系。 Camel仅使用JMSCorrelationID来关联消息吗?
  2.   

不完全是。 camel-jms(以及正在扩展它的camel-activemq)组件标头JMSCorrelationID到标头CamelCorrelationId

pollEnrich中的相关性再次与CamelCorrelationId合作,而camel-jms将此标头转换为消息选择器JMSCorrelationID=CamelCorrelationId

是的,在camel-jms(和继承的组件)端点与exchangePattern=InOnly之间进行通信时,将在标头JMSCorrelationID上有效地进行关联。

请参阅Apache Camel: Correlation Identifier

  
      
  1. 当将InOnly消息发送到请求源自的另一个队列(回复)时,Camel设法保留JMSCorrelationID。这是可取的,但它实际上是如何做到的?保留JMSCorrelationID
  2. 具体规则是什么?   

使用JMSCorrelationID时,Camel对exchangePattern=InOnly进行了转换。

在输入路线上,Camel从输入javax.jms.Message获取相关ID并将其保存到org.apache.camel.Message#headers.JMSCorrelationId

在输出路线上,Camel从org.apache.camel.Message#headers.JMSCorrelationId获取相关ID并将其设置为javax.jms.Message.setJMSCorrelationID()

注意:我不是Apache Camel的开发人员。我在这个答案中写的每个字都是基于使用这个框架时的用户体验。答案可能存在细微的差异。