Reputation: 10502
Is there some issue with paho java client when connect with username & password ??
From paho JS client it works but from java client it doesn't. I have this code
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();
}
});
i am getting this exception
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([email protected]:50741): Username 'undefined' login failed for username_or_password_undefined
06:47:36.463 [error] Client([email protected]:50742): Username 'undefined' login failed for username_or_password_undefined
According paho documentation public void setPassword(char[] password) So here i am passing char[] as paasword
I am using emqttd
broker
Upvotes: 2
Views: 2167
Reputation: 7633
here is a code that publish to a mqtt broker using eclipse paho mqtt library.
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();
}
}
}
Upvotes: 1
Reputation: 48297
Is there some issue with paho java client when connect with username & password ??
nop. there is not, it looks like your user name and password is not in the CAL of the broker, you need to verify that your credentials for the Authentication are ok.
and that the broker has the ACL right
Upvotes: 0