2015-02-10 172 views
1

爲了演示Paho MQTT,我已經在Java中下載了一個示例。當我運行它發生Paho MQTT拋出異常

public class Thermometer { 

    public static final String BROKER_URL = "tcp://test.mosquitto.org:1883"; 

    public static final String TOPIC = "xyz.abc"; 

    private MqttClient client; 


    public Thermometer() { 
     try { 
      MemoryPersistence per = new MemoryPersistence(); 
      String clientId = UUID.randomUUID().toString(); 
      client = new MqttClient(BROKER_URL, clientId, per); 
     } catch (MqttException e) { 
      e.printStackTrace(); 
      System.exit(1); 
     } 
    } 

問題,它定位在與client = new MqttClient(BROKER_URL, clientId, per);

異常在線程 「主」 java.lang.IllegalArgumentException異常 在org.eclipse.paho.client.mqttv3.MqttClient。 (MqttClient.java:170) 在mqtt_pub.Thermometer。(Thermometer.java:26) 在mqtt_pub.Thermometer.main(Thermometer.java:65)

我發現@拋出IllegalArgumentException如果QoS的值不是0,1或2,但在類MemoryPersistence中他們沒有提到。請提前幫助,謝謝。

+0

也許你正在使用一個相當老版本的paho mqtt庫 - 那麼,你使用的是哪個版本? – nos 2015-02-10 19:53:06

+0

我使用了paho 1.0.1版本。但我在http://www.eclipse.org/paho/files/javadoc/index.html?org/eclipse/paho/client/mqttv3/IMqttClient.html閱讀它的文檔,也許它是mqttv3 – Bryan 2015-02-10 19:57:51

回答

0

如果您看看MttqClientsource code,您可以看到uuid只能有最大23個字符的長度。看起來像的uuid較長:

if (clientId == null || clientId.length() == 0 || clientId.length() > 23) 
{ 
     throw new IllegalArgumentException(); 
} 

UUID.randomUUID().toString()返回與炭36的長度的字符串;

+0

謝謝,它的工作原理 – Bryan 2015-02-10 19:56:15