2016-01-22 64 views
0

我很難跟蹤我的駱駝路由問題。從我一直在讀的東西看來,它似乎可能與我的路由密鑰頭信息搞砸了,但林不知道如何解決這個問題。另外,如果這很重要,那麼這是一個Java OSGi項目,但所有Camel的東西目前都是用XML實現的。任何幫助表示讚賞。使用RabbitMQ和Apache Camel獲取AMQP消息的錯誤路由密鑰

這裏就是我試着做到:

<!-- 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> 

此代碼似乎已工作的罰款。問題就出現了,當我們添加新的組合,這也是聽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> 

這條路線失敗,它試圖錯誤消息在addedTwo路線中將secondObject拆分爲firstObject

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 」。但我沒有明確地告訴它在代碼中的任何地方做 - 任何想法?

回答

2

在RabbitMQ的駱駝組成部分,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