使用RabbitMQ和Apache Camel获取AMQP消息的错误路由密钥

时间:2016-01-22 18:20:22

标签: rabbitmq apache-camel

我很难跟踪Camel路线的问题。从我一直在阅读,似乎它可能与我的路由键标题信息搞砸了,但我不知道如何解决这个问题。此外,这是一个Java OSGi项目,如果这很重要,但所有Camel的东西目前都是用XML实现的。任何帮助表示赞赏。

这就是我想要做的事情:

<!-- The first route creates an object with some info in it and drop it on a rabbitmq 
exchange called message.added -->
<route id="directIn">
    <from uri="direct:in" />
    <bean ref="connector" method="handleIncoming" />
    <marshal id="marshal-one" ref="firstObject" />
    <to uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true&amp;routingKey=message.added" />
</route>

<!-- This route listens to message.added, processes the data, creates a new object, and 
drops it on a different rabbitmq exchange called message.rest -->
<route id="addedOne">
    <from uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true&amp;routingKey=message.added" />
    <unmarshal id="unmarshal-one" ref="firstObject" />
    <bean ref="connector" method="processAndConvert" />
    <marshal id="marshal-out" ref="secondObject" />
    <to uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true&amp;routingKey=message.rest" />
</route>

此代码似乎一直运行良好。当我们添加一个新的bundle时会出现问题,该bundle也正在收听message.added rabbitmq消息(我相信你可以这样做吗?)

<!-- This route is in a different bundle, also listening to message.added, processing
     the data and creating the same object (from a common bundle) and dropping it
     on the same rabbitmq exchange as before -->
<route id="addedTwo">
    <from uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true&amp;routingKey=message.added" />
    <unmarshal id="unmarshal-two" ref="firstObject" />
    <bean ref="someService" method="processUpdate" />
    <marshal id="marshal-out2" ref="secondObject" />
    <to uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true&amp;routingKey=message.rest" />
</route>

此路由失败,并显示一条错误消息,指出它正在尝试将secondObject解组为firstObject路由中的addedTwo

ERROR | RabbitMQConsumer | DefaultErrorHandler ....
caught: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "secondObject" (class com.pointer.dangling.FirstObject["secondObject"])

Message History:
RouteId        ProcessorId        Processor
[addedTwo]     [addedTwo]         [                             ]
[addedTwo]     [unmarshal-two]    [unmarshal[ref:firstObject]   ]

Exchange:
Headers: {
    CamelRedelivered=false,
    rabbitmq.DELIVERY_TAG=1,
    rabbitmq.EXCHANGE_NAME=me.ex,
    rabbitmq.ROUTING_KEY=message.added
}
Body: {
    "secondObject": {
        // a bunch of fields for secondObject
    }
}

似乎'secondObject'在某个时刻进入'message.added'交易所并被'addedTwo'路线选中,该路线试图将其编组为'firstObject'。但我没有明确告诉它在代码中的任何地方 - 任何想法?

1 个答案:

答案 0 :(得分:2)

在RabbitMQ Camel组件中,routingKey端点选项仅适用于消费者<from />)。

生产者必须将其路由密钥明确设置为<to />之前的邮件标头。您的addedOne路线应如下所示:

<route id="addedOne">
    <from uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true&amp;routingKey=message.added" />
    <unmarshal id="unmarshal-one" ref="firstObject" />
    <bean ref="connector" method="processAndConvert" />
    <marshal id="marshal-out" ref="secondObject" />
    <setHeader headerName="rabbitmq.ROUTING_KEY">
        <constant>message.rest</constant>
    </setHeader>
    <to uri="rabbitmq://localhost:5672/me.ex?exchangeType=topic&amp;durable=false&amp;autoDelete=true" />
</route>

有关详细信息,请参阅https://camel.apache.org/rabbitmq.html