5

I'm trying to implement a JAVA application, with the aim to publish into a specific MQTT topic. The message should be delivered with QoS 2 (delivered exactly once).

But I seem to forget anything in my implementation (code of a JUnit implementation below), so the messages always seem to be delivered though there's no client subscribed to my topic. Does anyone have an idea what's my fault here?

I'm using a mosquitto MQTT broker on Ubuntu 12.04 and Eclipse Paho on 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 {
            // No part of that test
        }

        @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) {
            // Not part of that test
        }
    });

    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) {
            // Expected behaviour
        }
    });

    token.waitForCompletion();

    assertEquals(true, token.isComplete()); 
    assertNotNull(token.getException());        // Should be not null due to unsuccessful delivery with QoS 2
justus
  • 584
  • 1
  • 7
  • 19
  • 4
    QoS 2 is giving you a guarantee about delivery to the broker, not a subscriber. – ralight Oct 13 '14 at 14:59
  • Ok, then I just misunderstood the documentation (i.e. http://www.eclipse.org/paho/files/mqttdoc/Cclient/qos.html). I thought the "receiver" has to be a mqtt client, not the broker. Is there any possibiliy to gauarantee that any client received my message? – justus Oct 13 '14 at 15:11
  • The receiver could be a client - you could have communications from broker to client as well. They are separate events though. You could do what you want at the application level - i.e. send a message, then the other end sends another message to confirm. – ralight Oct 13 '14 at 16:13
  • Hm, it was my aim not to do this at application level, but with a bit of extra work this should be possible... Thank you very much for your help! – justus Oct 13 '14 at 16:41

0 Answers0