【问题标题】:Program hangs while publishing message using qos = 0 in MQTT java在 MQTT java 中使用 qos = 0 发布消息时程序挂起
【发布时间】:2018-04-02 08:46:45
【问题描述】:

我正在 thingstream 中处理 mqtt 客户端。当我尝试使用 qos = 0 将消息发布到主题时,消息已发送,但程序仅挂在那里。然后我尝试使用 qos = 1 发送消息。消息发送成功,程序也没有挂起。但是在发送另一条消息时,我收到中断异常。谁能帮我吗。

下面是我用来发布消息的sn-p。

MqttMessage message = new MqttMessage(command);
message.setPayload(command);
message.setQos(1);
mqttClient.publish(topic, message);
System.out.println("Message published");

使用的库 - o​​rg.eclipse.paho.client.mqttv3-1.2.0

mqtt客户端初始化代码

public void connect() {

    try {
        mqttClient = new MqttClient(serverUri, clientId);
        //mqttClient.setTimeToWait(10000);
    } catch (MqttException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    mqttClient.setCallback(new MqttCallbackExtended() {
        @Override
        public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
            // TODO Auto-generated method stub
            System.out.println("messageArrived: " + topic.toString());
            System.out.println(mqttMessage.toString());
            System.out.println(mqttMessage.getPayload());
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken arg0) {
            // TODO Auto-generated method stub
            System.out.println("deliveryComplete: " + arg0.getMessageId());
        }

        @Override
        public void connectionLost(Throwable arg0) {
            // TODO Auto-generated method stub
            System.out.println("---Connection lost1");
            // Toast.makeText(App.getContext(), "Connection
            // lost",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void connectComplete(boolean arg0, String arg1) {
            // TODO Auto-generated method stub
            System.out.println("connectComplete");
            try {
                mqttClient.subscribe("device/+/publish");
            } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                commands.sendCommand(Commands.GET_STATUS, null, "device/identity:85111741-5789-3010-85c9-be4a7204e5d3");
            } catch (MqttException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setAutomaticReconnect(true);
    mqttConnectOptions.setCleanSession(true);
    mqttConnectOptions.setUserName(username);
    mqttConnectOptions.setPassword(password.toCharArray());
    mqttConnectOptions.setKeepAliveInterval(30);
    mqttConnectOptions.setConnectionTimeout(60);
    mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);

    try {
        mqttClient.connect(mqttConnectOptions);
    } catch (MqttSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MqttException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

【问题讨论】:

    标签: java mqtt


    【解决方案1】:

    不要在主线程中发送,而是使用另一个线程来发布消息。

    Thread thread = new Thread() {
       public void run() {
           mqttClient.publish(topic, message);
       }  
    };
    thread.start(); 
    

    【讨论】:

      【解决方案2】:

      问题实际上可能不是 qos 值,而是您的客户端与 MQTT 服务器的连接是否良好/稳定。如前所述herepublish

      ... 是一个阻塞方法,一旦发布完成就返回

      发布可能未完成。检查您是否连接到您的 MQTT 服务器,以及您的 MQTT 客户端是否已成功连接到 MQTT 服务器。

      您也可以在调用周围加上try... catch ... 语句来查看是否有异常被抛出:

      try {
        mqttClient.publish(topic, message);
      } catch(Exception e) {
        e.printStackTrace();
      }
      

      【讨论】:

      • 我也试过了。异常中没有显示任何内容
      • 您可能需要发布更多代码。我怀疑您与服务器的连接、发送和/或接收数据的方式存在问题,而不是 MQTT……也许有什么东西正在关闭/保持您与 MQTT 服务器的连接。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多