Paho MQTT 抛出异常

Paho MQTT throws Exception

为了演示 Paho MQTT,我已经在 Java 下载了一个示例。

 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);

Exception in thread "main" java.lang.IllegalArgumentException at org.eclipse.paho.client.mqttv3.MqttClient.(MqttClient.java:170) at mqtt_pub.Thermometer.(Thermometer.java:26) at mqtt_pub.Thermometer.main(Thermometer.java:65)

我发现如果 QoS 的值不是 0、1 或 2,但在 class MemoryPersistence 中他们没有提到,@throws IllegalArgumentException。请帮忙,提前谢谢你。

如果您查看 MttqClientsource code,您会发现 uuid 最多只能有 23 个字符的长度。看起来 uuid 更长:

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

UUID.randomUUID().toString() returns 长度为 36 个字符的字符串;