I am trying to implement a JAVA application in order to publish in a specific MQTT topic. The message must be delivered with QoS 2 (delivered exactly once).
But I seem to have forgotten something in my implementation (the JUnit implementation code is below), so the messages always seem delivered, although there is no client for my question. Does anyone have an idea that my mistake is here?
I use the mosquitto MQTT broker on Ubuntu 12.04 and Eclipse Paho on the JAVA side.
MqttAsyncClient client = new MqttAsyncClient("tcp://localhost:1883", MqttClient.generateClientId(), new MemoryPersistence());
try {
client.connect().waitForCompletion();
}
catch (MqttException e) {
throw new RuntimeException("Failed to connect message-broker. Maybe it has to be started via typing \"sudo mosquitto\" in a new terminal window.");
}
client.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
throw new RuntimeException("Message with QoS 2 marked as delivered, but no client subscribed to topic.");
}
@Override
public void connectionLost(Throwable cause) {
}
});
IMqttDeliveryToken token = client.publish("just/another/topic/where/nobody/is/listening", "Important message with QoS 2".getBytes(), 2, false, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
throw new RuntimeException("Message with QoS 2 marked as delivered, but no client subscribed to topic.");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
}
});
token.waitForCompletion();
assertEquals(true, token.isComplete());
assertNotNull(token.getException());
source
share