I want to publish an MQTT message using Eclipse's Paho library, but EOFException and connection lost exceptions are occurring. I have tried changing the broker url to ssl or tcp but neither are working. There are 2 methods which I have written. One is working fine, but the second one with Paho is not working, and I want subscribe using Paho. What's wrong with my code?
Below 1st method is working fine:
String clientEndPoint = "sedhhytjkss-ats.iot.ap-southeast-1.amazonaws.com";
String clientId = "smart_pen";
String awsAccessKeyId = "DRSFGDFKLSLDSP";
String awsSecretAccessKey = "/HHJKlkKLDddkdldsksdpsk255s6";
@GetMapping("/")
public void publishMessage() throws IOException, AWSIotException {
String topic = "$aws/things/smart_pen/shadow/update";
String state = "{\"state\":{\"reported\":{\"smart_pen\":3.0}}}";
AWSIotQos qos = AWSIotQos.QOS0;
String payload = "first message";
long timeout = 3000;
AWSIotMqttClient client = new AWSIotMqttClient(clientEndPoint, clientId,
awsAccessKeyId, awsSecretAccessKey,
null);
AWSIotDevice device = new AWSIotDevice(clientId);
client.attach(device);
client.connect();
MyMessage message = new MyMessage(topic, qos, payload.toString());
client.publish(message, timeout);
}
The below 2nd method not working :
private static final String BROKER = "sedhhytjkss-ats.iot.ap-southeast-1.amazonaws.com";
private static final String CLIENT_ID = "smart_pen";
private static final String TOPIC = "$aws/things/smart_pen/shadow/update";
private static final String AWS_ACCESS_KEY = "DRSFGDFKLSLDSP";
private static final String AWS_SECRET_KEY = "/HHJKlkKLDddkdldsksdpsk255s6";
@GetMapping("/")
public void publishMessageWithPaho() {
try {
MqttClient client = new MqttClient(BROKER, CLIENT_ID);
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(AWS_ACCESS_KEY);
options.setPassword(AWS_SECRET_KEY.toCharArray());
client.connect(options);
MqttMessage message = new MqttMessage();
message.setPayload("First message".getBytes());
message.setQos(0);
client.publish(TOPIC, message);
client.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}