5

特定の MQTT トピックに公開することを目的として、JAVA アプリケーションを実装しようとしています。メッセージは QoS 2 で配信する必要があります (1 回だけ配信されます)。

しかし、実装 (以下の JUnit 実装のコード) で何かを忘れているように見えるため、トピックにサブスクライブしているクライアントがなくても、メッセージは常に配信されているように見えます。誰かがここで私のせいだと思いますか?

Ubuntu 12.04 で mosquitto MQTT ブローカーを使用し、JAVA 側で Eclipse Paho を使用しています。

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
4

0 に答える 0