MQTT发布在1小时后挂起 - QOS-0,有限的资源,Java Paho Client

时间:2017-05-10 02:12:37

标签: java mqtt hang paho qos

我使用简单的MQTT客户端使用Paho客户端java进行发布和订阅。我有大约256MB或更少RAM的资源(具有降低功能的Linux机器)。我的QOS级别为0.我与mqtt broker的连接成功。我能够成功地向经纪人发送消息一小时。我的消息率是< = 1msg / s。一个小时后,我的应用程序就挂了。我在StackOverflow和其他地方读了几篇文章,并将setKeepAliveInterval设置为120s,将setTimeToWait设置为1s(我将它从2分钟变为1秒)。我尝试过async客户端,同步客户端有和没有waitToComplete,但它在所有情况下都可以重现。我已经使用了MemoryPersistence和File持久性 - 在这两种情况下都可以重现。我也尝试过topic.publish和client.publish - 在这两种情况下都可以重现。 以下是我的代码段:

@Override
  public void connect() {
    SSLContext sslContext = setSSLContext();

    connOpt = new MqttConnectOptions();
    connOpt.setSocketFactory(sslContext.getSocketFactory());
    connOpt.setCleanSession(true);
    connOpt.setUserName(userName);
    connOpt.setPassword(password);

    connOpt.setKeepAliveInterval(MQTT_KEEP_ALIVE_TIMEOUT_SECS);
    // Connect to Broker
    int retry = 1;
    while (retry <= MQTT_CONNECT_RETRIES) {
      try {
        client = new MqttClient("ssl://" + mqtt.getMqttBrokerIp(), someId, memPersistence);
        client.setCallback(callback);
        client.connect(connOpt);
        client.setTimeToWait(1000);
        logger.info("msg='Connection to mqtt broker successful'");
        return;
      } catch (MqttException e) {
        logger.warn(
            "err='Connection to mqtt broker failed', retryNumber={}, errMessage={}, errStack={}",
            retry, e.getMessage(), e.getStackTrace());
      }
      retry++;
    }
    logger.error("err='Connection to mqtt broker failed'");
  }

  @Override
  public void publish(String topicPublish, String message) {
    MqttMessage mqttMsg = new MqttMessage(message.getBytes());
    mqttMsg.setQos(QOS);
    mqttMsg.setRetained(false);
    try {
      client.publish(topicPublish, mqttMsg);
    } catch (MqttPersistenceException e) {
      logger.error("errMessage={}, errStack={}", e.getMessage(), e.getStackTrace());    
    } catch (MqttException e) {
      logger.error("errMessage={}, errStack={}", e.getMessage(), e.getStackTrace());    
    }
    /*
    MqttMessage mqttMsg = new MqttMessage(message.getBytes());
    MqttTopic topic = client.getTopic(topicPublish);
    mqttMsg.setQos(QOS);
    mqttMsg.setRetained(false);
    try {
      //memPersistence.clear();
      MqttDeliveryToken token = topic.publish(mqttMsg);
      token.waitForCompletion();
    } catch (MqttPersistenceException e) {
      logger.error("errMessage={}, errStack={}", e.getMessage(), e.getStackTrace());    
    } catch (MqttException e) {
      logger.error("errMessage={}, errStack={}", e.getMessage(), e.getStackTrace());    
    }
    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      logger.debug(" exception ");   
    }
    */
  }

  @Override
  public void subscribe(String topicSubscribe) {
    try {
      client.subscribe(topicSubscribe, QOS);
    } catch (MqttException e) {
      logger.error("errMessage={}, errStack={}", e.getMessage(), e.getStackTrace());
    }
  } 

我的代码运行1小时左右,然后在进入deliveryComplete()之前总是挂起。我在那里登录以检查哪些从未打印过。我的代码依赖于以下日志:

org.eclipse.paho.client.mqttv3.internal.Token notifyComplete FINE: 2384: >key=2061 response=null excep=null

我正在使用

存储库中的

maven { url 'https://repo.eclipse.org/content/repositories/paho-releases/' }和我的build.gradle中的依赖项中的compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'。我也试过

maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
} 

在我的存储库中。它没有帮助。任何人都可以建议IK可能做错了什么。我已经经历了很多例子和帖子,之后我在这里发帖。

0 个答案:

没有答案