2014-06-23 94 views
1

我有一個使用主題和隊列的activeMQ 5.9.1的Java Swing應用程序。如何使用MQTT(paho)+ activeMQ接收消息的正確方法?

現在,我的意圖是將該應用程序遷移到Web,所以我正在使用activeMQ + MQTT(paho)JavaScript庫進行一些證明。

我已經啓用,在activemq.xml中:

<transportConnector name="mqtt+ws" uri="ws://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600&amp;transport.defaultKeepAlive=30000"/> 

而且我已經實現了與MQTT(PAHO - http://eclipse.org/paho/clients/js/)一些例子聽一些議題。

function ramdomID(length) { 
    var text = ""; 
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
    for(var i=0; i<length; i++) { 
     text += possible.charAt(Math.floor(Math.random() * possible.length)); 
    } 
    return text; 
} 

var client = new Messaging.Client('192.168.240.17', 1883, ramdomID(20)); 

client.onConnectionLost = onConnectionLost; 
client.onMessageArrived = onMessageArrived; 
client.connect({onSuccess:onConnect}); 

function onConnect() { 
    console.log("onConnect"); 
    client.subscribe("/KeepAlive"); 
} 

function onConnectionLost(responseObject) { 
    if (responseObject.errorCode !== 0) { 
     console.log("onConnectionLost: "+responseObject.errorMessage); 
    } 
} 

function onMessageArrived(message) { 
    console.log("onMessageArrived: "+message.payloadString); 
} 

到主題的答覆被正確接收,但恢復的消息是一個Java String對象引用:

onMessageArrived: ﭭsr<com.my.project.bp.jms.MyImplementedMessageLmessagetLjava/lang/String;Ltypeq~xppt KeepAlivey 

任何人都知道正確的方式,如果一個Java應用程序寫入到收到一個友好的消息activeMQ主題直接?

回答

0

如果你想從ActiveMQ轉到瀏覽器,看看只啓用ActiveMQ中的Websockets,並使用WebSockets JavaScript庫上的STOMP。消息的主體將是一個友好的文本對象。鏈接在我的答案在這裏:Best way to display dynamic data in a webpage

+0

感謝您的答覆。但是我通過Websockets使用MQTT。我發現我的錯誤:在服務器端,我有一些使用JMS的函數,他們使用javax.jms.ObjectMessage對象來編寫主題。將該對象更改爲javax.jms.TextMessage並在JSON中設置消息一切正常! –