websocket mqtt,内置activeMQ

时间:2015-04-07 08:55:34

标签: servlets websocket jetty activemq mqtt

我将一个ActiveMQ代理设置为tcp和websockets上的MQTT-Broker,如下所示:

BrokerService broker = new BrokerService();
broker.addConnector("mqtt://localhost:1883");
broker.addConnector("ws://0.0.0.0:1884");
broker.setPersistent(false);
broker.setBrokerName("default");
broker.start();

在我的类路径中,我有以下罐子(因为我不想包含slf4j):

activemq-broker-5.11.1.jar          activemq-stomp-5.11.1.jar
activemq-client-5.11.1.jar          activemq-web-5.11.1.jar
activemq-http-5.11.1.jar            commons-codec-1.6.jar
activemq-mqtt-5.11.1.jar            geronimo-j2ee-management_1.1_spec-1.0.1.jar
activemq-openwire-legacy-5.11.1.jar     geronimo-jms_1.1_spec-1.1.1.jar
activemq-parent-5.11.1              hawtbuf-1.11.jar
activemq-spring-5.11.1.jar          mqtt-client-1.10.jar
javax.servlet-api-3.1.0.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1-2.jar
jetty-all-9.2.8.v20150217.jar
slf4j-api-1.7.5
logback-classic-1.0.13
logback-core-1.0.13

当我尝试连接paho javascript客户端时,它挂起,而我的mosquitto客户端设法通过tcp连接,没有麻烦。

在日志中我找到了这一行:

DEBUG o.a.a.t.ws.jetty9. MQTTSocket - Waiting for StompSocket to be properly started...

读取代码我发现MQTTSocket(与StompSocket相同;尝试过两者)正在等待一个只能在doStart()中倒计时的CountDownLatch对象。显然doStart()永远不会被调用。为什么?我怎么称呼它?我错过了什么?

我的html / javascript:

<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <script src="shared/mqttws31-min.js" type="text/javascript"></script>
  <script type="text/javascript">

    //sample HTML/JS script that will publish/subscribe to topics in the Google Chrome Console
    //by Matthew Bordignon @bordignon on twitter.

    var wsbroker = "127.0.0.1";  //mqtt websocket enabled broker
    var wsport = 1884 // port for above

    var client = new Paho.MQTT.Client(wsbroker, wsport, "",
        "myclientid_" + parseInt(Math.random() * 100, 10));

    client.onConnectionLost = function (responseObject) {
      console.log("connection lost: " + responseObject.errorMessage);
    };

    client.onMessageArrived = function (message) {
      console.log(message.destinationName, ' -- ', message.payloadString);
    };

    var options = {
      timeout: 300,
      //mqttVersion: 3, // to constrain the protocol to version 3
      onSuccess: function () {
        console.log("mqtt connected");
        // Connection succeeded; subscribe to our topic, you can add multile lines of these
        client.subscribe('/World', {qos: 1});


        //use the below if you want to publish to a topic on connect
        message = new Paho.MQTT.Message("Hello");
        message.destinationName = "/World";
        client.send(message);

      },
      onFailure: function (message) {
        console.log("Connection failed: " + message.errorMessage);
      }
    };

  function init() {
      client.connect(options);
  }

    </script>
  </head>
  <h1>MQTT test</h1>
  <body onload="init();">
  </body>

</html>

修改: 我还尝试使用随ActiveMQ附带的改编xml文件的xbeans配置:

System.setProperty("activemq.conf", "conf");
System.setProperty("activemq.data", "data");

BrokerService broker;
try {
    broker = BrokerFactory.createBroker("xbean:activemq.xml");
    broker.start();
} catch (Exception e1) {
    e1.printStackTrace();
}

XML文件:

2 个答案:

答案 0 :(得分:0)

  1. 我想broker.addConnector("ws://0.0.0.0:1884"); 应为broker.addConnector("ws://localhost:1884");

  2. 我写过并成功运行BrokerService mqtt 以及 ws ,我怀疑你错过了{{} 1}}

  3. 请注意,websocket-serverjetty-all版本取决于websocket-server版本。这些是我的 gradle 依赖项:

    activemq-http
    1. 您可以使用以下命令指定单个连接器属性,例如名称:
    2. TransportConnector connector = new TransportConnector();

        compile group: "org.apache.activemq",  version: "5.11.+", name: "activemq-all"
      
        compile group: "org.apache.activemq",  version: "5.11.+", name: "activemq-mqtt"
      
        compile group: "org.apache.activemq",  version: "5.11.+", name: "activemq-http"
      
        compile group: "org.eclipse.jetty.aggregate",  version: "8.+", name: "jetty-all"  
      
        compile group: "org.eclipse.jetty.websocket",  version: "9.+", name: "websocket-server"  
      

答案 1 :(得分:0)

我在activemq中添加了4行代码,现在可以使用了。还不确定它是否会被包括在内,至少我是在bug追踪器中发布的:
https://issues.apache.org/jira/browse/AMQ-5784
补丁:http://pastebin.com/phcsJHR5