Paho java客户端无法连接:用户名或密码错误(4)

时间:2017-04-10 03:32:03

标签: mqtt iot paho

与用户名和用户连接时,paho java客户端是否存在问题?密码??

从paho JS客户端可以工作但是从java客户端却没有。 我有这个代码

MqttConnectOptions conOpt = new MqttConnectOptions();
conOpt.setCleanSession(false);
conOpt.setUserName("test5");
conOpt.setPassword("123".toCharArray());
MqttDefaultFilePersistence filePersistence = new MqttDefaultFilePersistence("/home/manish/Downloads/mqttPersist");
client = new MqttAsyncClient(appProps.getProperty("mqtt.broker"),
        appProps.getProperty("mqtt.clientId"), filePersistence);
client.setCallback(this);
client.connect(conOpt, new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken imt) {
        try {
            client.subscribe(Constants.INTERNAL_TOPICS, Constants.INTERNAL_TOPIC_QOS);
        } catch (MqttException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void onFailure(IMqttToken imt, Throwable thrwbl) {
        thrwbl.printStackTrace();
    }
});

我收到此异常

Bad user name or password (4)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:885)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:118)
at java.lang.Thread.run(Thread.java:745)
emqt console

06:47:36.456 [error] Client(notification_subs_bot@127.0.0.1:50741): Username 'undefined' login failed for username_or_password_undefined
06:47:36.463 [error] Client(notification_subs_bot@127.0.0.1:50742): Username 'undefined' login failed for username_or_password_undefined

根据paho文档public void setPassword(char [] password)所以这里我将char []作为paasword传递

我正在使用emqttd经纪人

2 个答案:

答案 0 :(得分:0)

  

连接用户名& 时,paho java客户端是否存在问题    密码??

NOP。没有,看起来您的用户名和密码不在代理的CAL中,您需要验证您的身份验证凭据是否正常。

且经纪人的 ACL 正确

答案 1 :(得分:0)

这是使用eclipse paho mqtt库发布到mqtt代理的代码。

import com.fasterxml.jackson.core.JsonProcessingException;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class GpsPublish {

    public static void main(String[] args) {


        String uri = "tcp://000.000.000.000:1883";
        String deviceName = "SENSE2";
        String deviceToken = "IHAUD6kvW6C1Zp10uJm4";
        String topic = "v1/devices/me/telemetry";
        String clientId = MqttAsyncClient.generateClientId();
        MqttClientPersistence persistence = new MemoryPersistence();

        ObjectMapper MAPPER = new ObjectMapper();
        String jsonString = "{\"latitude\":51.520008, \"longitude\":13.304954, \"temperature\":31.5, \"active\": false}";

        try {
            MqttAsyncClient client = new MqttAsyncClient(uri, clientId, persistence);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(deviceToken);


            client.connect(options, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken iMqttToken) {
                    System.out.println("[{" + deviceName + "}] connected to Thingsboard!");
                }

                @Override
                public void onFailure(IMqttToken iMqttToken, Throwable e) {
                    System.out.println("[{" + deviceName + "}] failed to connect to Thingsboard!");
                    // System.out.println("reason " + e.getReasonCode());
                    System.out.println("msg " + e.getMessage());
                    System.out.println("loc " + e.getLocalizedMessage());
                    System.out.println("cause " + e.getCause());
                    System.out.println("excep " + e);
                    e.printStackTrace();
                }
            }).waitForCompletion();

            JsonNode data = MAPPER.readTree(jsonString);
            MqttMessage msg = new MqttMessage(MAPPER.writeValueAsString(data).getBytes(StandardCharsets.UTF_8));

            IMqttDeliveryToken deliveryToken = client.publish(topic, msg, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    System.out.println("Data updated!");
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    System.out.println("[{" + deviceName + "}] Data update failed!");
                    System.out.println(exception.getMessage());
                }
            });
            deliveryToken.waitForCompletion();
            System.out.println("[{" + deviceName + "}] delivery completed at Thingsboard!");

            client.disconnect().waitForCompletion();
            System.out.println("[{" + deviceName + "}] disconnected to Thingsboard!");
        } catch (MqttException e) {
            System.out.println("Failed to connect to the server: " + e);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}