如何使用MQTT(paho)+ activeMQ接收消息的正确方法?

问题描述:

我有一个使用主题和队列的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主题直接?

如果你想从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中设置消息一切正常! –