连接到Mqtt在线代理时出现NullPointerException

时间:2019-03-05 13:25:45

标签: java mqtt mosquitto paho

我正在尝试使用以下代码和Java中的Paho库连接到在线代理https://test.mosquitto.org/

private final String brokerURI = "test.mosquitto.org:1883"; //should be changed to 8883 with SSL
try { //tentativo di creazione del client
        client = new MqttClient(brokerURI, idClient); <--NullPointerException here
        client.setCallback(new ClientCallback(codaTopic, codaMessaggi, finestra)); //set delle callback
        setConnectionOptions(); //set delle opzioni connessione
        client.connect(opzioni); //connessione al server
    } catch (MqttException e) {
        System.err.println(e.getMessage());
        System.err.println("Connessione fallita Client, riavviare il sistema.");
    }

在此处设置连接选项:

    private void setConnectionOptions() {
    opzioni = new MqttConnectOptions();
    opzioni.setAutomaticReconnect(true);
    opzioni.setCleanSession(false);
    opzioni.setConnectionTimeout(30);
    opzioni.setKeepAliveInterval(60);
}

,但是在创建NullPointerException时它将继续显示MqttClient。控制台特别显示:

Exception in thread "Thread-3" java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:489)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:291)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:185)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:226)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:138)
at client.Client.run(Client.java:78)

我如何管理连接和使用SSL? 上网浏览本教程或指南均无济于事,我已经下载了mosquitto.org.crt文件用于SSL连接,但是我不知道在哪里使用它,也没有找到教程。

编辑
将BrokerUri更改为
private final String brokerURI = "tcp://test.mosquitto.org:1883"; //indirizzo broker 控制台显示错误

Client non connesso (32104)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:166)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:835)
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:322)
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:315)
at client.Client.subscribe(Client.java:214)
at client.Client.run(Client.java:89)

尝试通过说明订阅主题

client.subscribe(topic, 1);

topic参数是一个包含主题名称的字符串。

1 个答案:

答案 0 :(得分:3)

Mosquitto的URI需要协议。看一下它的源代码,这是引发您的异常的地方,类MqttConnectOpts.java

protected static int validateURI(String srvURI) {
    try {
        URI vURI = new URI(srvURI);
        if (!vURI.getPath().equals("")) {
            throw new IllegalArgumentException(srvURI);
        }
        if (vURI.getScheme().equals("tcp")) {
            return URI_TYPE_TCP;
        }
        else if (vURI.getScheme().equals("ssl")) {
            return URI_TYPE_SSL;
        }
        else if (vURI.getScheme().equals("local")) {
            return URI_TYPE_LOCAL;
        }
        else {
            throw new IllegalArgumentException(srvURI);
        }
    } catch (URISyntaxException ex) {
        throw new IllegalArgumentException(srvURI);
    }
}

因此,它接受3种类型的协议前缀:tcp, ssl, local。对于您的示例,您可以这样尝试:

  • TCP

     private final String brokerURI = "tcp://test.mosquitto.org:1883";
    
  • SSL

    private final String brokerURI = "ssl://test.mosquitto.org:8883";