2015-06-03 84 views
1

我想發佈文本消息的主題使用apache與activemq駱駝。 以下代碼正常工作,我的客戶端程序能夠將消息轉換爲文本消息。apache駱駝多播轉換文本消息字節消息

<route id="setText"> 
       <from uri="restlet:///test/{testId}?restletMethod=POST" /> 
       <setExchangePattern pattern="InOnly" /> 
       <setBody> 
        <simple>${header.testId}:${body}</simple> 
       </setBody> 
       <to uri="activemq:topic:TestTopic" /> 
      </route> 

現在我添加了多播來完成多項操作。多播能夠很好地發揮作用,併成功地向主題發送消息。

<route id="setText"> 
         <from uri="restlet:///test/{testId}?restletMethod=POST" /> 
         <setExchangePattern pattern="InOnly" /> 
        <multicast> 
         <pipeline> 
         <!-- some operation --> 
         </pipeline> 
         <setBody> 
          <simple>${header.testId}:${body}</simple> 
         </setBody> 
         <to uri="activemq:topic:TestTopic" /> 
        </multicast> 
</route> 

但是,當向主題發送文本消息時,多播正在將消息轉換爲字節流。我的客戶端程序是無法消耗的消息轉換成TextMessage的bcoz消息是再見格式,下面是對的System.out.println顯示的信息(在我的客戶端程序)

ActiveMQBytesMessage {commandId = 5, responseRequired = true, messageId = ID:R-014-49827-1433324560754-3:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:R-014-49827-1433324560754-3:1:1:1, destination = topic://TestTopic, transactionId = null, expiration = 0, timestamp = 1433324582980, arrival = 0, brokerInTime = 1433324582981, brokerOutTime = 1433324583731, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = [email protected], marshalledProperties = [email protected], dataStructure = null, redeliveryCounter = 0, size = 0, properties = {breadcrumbId=ID-R-014-49826-1433324557692-2-1, org_DOT_restlet_DOT_http_DOT_version=1.1, testId=3100026, org_DOT_restlet_DOT_startTime=1433324582521, CamelHttpMethod=PUT, CamelHttpUri=http://localhost:8080/service-bus/test/3100026}, readOnlyProperties = true, readOnlyBody = true, droppable = false} ActiveMQBytesMessage{ bytesOut = null, dataOut = null, dataIn = null } 

您能否提供多播的原因是將短信轉換爲字節格式?以及如何發送文本格式的消息?提前致謝。

+0

多播肯定是*不*轉換anthying。你看錯了地方。 – EJP

+0

是的。我明白。它只是將輸入信息傳遞給所有子系統。但是當我用多播進行測試時,我的客戶端程序能夠將消息用作TextMessage。但是這在多播的情況下不會發生。 –

回答

0

我不確定你使用的是什麼版本的駱駝,但我看到了類似的問題,用jaxb dataformat將消息轉換爲字節。我會建議打開一個傑拉票。與此同時,我以前使用的一項工作是在jms端點上強制執行文本類型。

activemq:topic:TestTopic?jmsMessageType=Text 

參考: http://camel.apache.org/jms.html

0
我在與多播和JAXB解組相同的問題

。沒有多播,unmarshal()工作正常,我收到預期的對象類型作爲方法「Handler.received(Object)」的參數。 在多播中添加unmarshal(),而「Handler.received(Object)」取而代之的是Byte []。

// works - Handler.received(Object) receives correct object type: 
from("test-jms:queue:test.queue").unmarshal(jaxb).to("class:com.test.Handler?method=received"); 

// doesn't work - Handler.received(Object) receives a byte array: 
from("test-jms:queue:test.queue").multicast().unmarshal(jaxb).to("class:com.test.Handler?method=received"); 

我是一個完全新手駱駝,這一直讓我瘋狂的一整天。 上面建議的將jmsMessageType設置爲「Text」的解決方案對我來說也不起作用。在這種情況下,Handler類接收一個包含XML的字符串,就好像unmarshal()根本沒有做任何事情。

編輯: 我看着下面的StackOverflow問題: Apache camel multicast FreeMarker

而且改變了我的代碼以使用「管道」就像從用戶「克勞斯易卜生」的例子:

from("test-jms:queue:test.queue").multicast() 
.pipeline().to("file://targetdir/received").end() 
.pipeline().unmarshal(jaxb).to("class:com.test.Handler?method=received").end() 
.end(); 

而現在就像我期望的那樣工作。接收到的XML文件被複制到「targetdir/received」中,方法「Handler.received(Object)」獲取正確的對象類型作爲參數。 謝謝克勞斯! :)