使用PAHO失败的多个MQTT连接

时间:2017-06-09 09:39:57

标签: java mqtt messagebroker

我试图使用PAHO库维护两个MQTT连接。如果我维护相同的对象,则订阅会在第二个订阅时挂起,但如果我创建一个新的客户端,它可以工作但关闭第一个订阅。以下是我的代码段。我能错过什么?。

try {
        logging.info(logPreString + "| "
                + Thread.currentThread().getId() + " | "
                + "Subscribing to topic: ");

        // Construct the connection options object that contains connection parameters  
        connectOptions = new MqttConnectOptions();
        connectOptions.setCleanSession(true);
        connectOptions.setKeepAliveInterval(1000);

        // Construct an MQTT blocking mode client
        client = new MqttClient(broker, clientID, persistence);
        // Set this wrapper as the callback handler
        client.setCallback(this);

        // Connect to the MQTT server
        logging.info(logPreString + "Connecting to broker ..." + broker);
        client.connect(connectOptions);
        logging.info(logPreString + "Connected and sucscribed ");

        // Subscribe to a topic
        System.out.println("Subscribe to topic(s):  " + "| "
                + Thread.currentThread() + " | "
                + Arrays.toString(topic) + " With QOS of:" + props.getQoS());
        client.subscribe(topic, QOS);

    } catch (MqttException me) {
        logging.error(logPreString + "MqttException caught "
                + "while connecting to broker. Error: "
                + me.getMessage());
    }

}

public void subscribeAll(String[] topic, int[] QOS) {

    try {

        logging.info(logPreString + "subscribing to all ..." + broker);
        // Subscribe to a topic
        System.out.println("Subscribing to topic(s):  " + "| "
                + Thread.currentThread() + " | "
                + Arrays.toString(topic)
                + " With QOS of:" + props.getQoS());
        client.subscribe(topic, QOS);


    } catch (MqttException me) {
        logging.error(logPreString + "MqttException caught "
                + "while connecting to broker. Error: "
                + me.getMessage());
    } 
}

0 个答案:

没有答案