I have a new thread that creates a MqttAsyncClient instance and connects to a remote server. After connecting the client subscribes to a specific topic. If I use the MqttClient instead of the MqttAsyncClient I receive the messages, but if I use the MqttAsyncClient no messages are received. Below is my code, would someone please take a moment to see if I have something missing or incorrect?
public class MqttEventReceiver implements Runnable {
private static final String CLIENT_ID = UUID.randomUUID().toString();
private IMqttAsyncClient client = null;
public MqttEventReceiver(String apiStreamingUri, String
connectionAccessToken) {
}
private MqttCallback mqttCallback = new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) throws Exception {
String incomingMsg = new String(message.getPayload());
LOG.info("Message: ", new String(payload));
}
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
};
@Override
public void run() {
String tmpDir = System.getProperty("java.io.tmpdir");
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);
//make the connect request. this request establishes a permanent connection
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
options.setUserName("authorization");
options.setPassword(connectionAccessToken.toCharArray());
Long threadId = successfullyConnected();
client = new MqttAsyncClient(apiStreamingUri, CLIENT_ID, dataStore);
client.setCallback(mqttCallback);
client.connect(options).waitForCompletion();
client.subscribe("topic", 1).waitForCompletion();
}
}
Turns out it was the QoS setting causing the message to be delivered slowly. I set the QoS to 0 and the message was delivered promptly.