0
votes

Getting repeated retained messages when subscribing to a retained topic.

I used spring mqtt integration in my Iot project. Here once the retained message is received , it is continued in subscribing till I publish the blank message to the same topic with retained flag set as true. I have noticed that when I do the same process in terminal using mqtt commands such that subscribing to the retained topic, it subscribes only once, no repeated subscription happens.

I used the below code for subscribing all topics by using #

@Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }
    @Bean
    public DefaultMqttPahoClientFactory clientfactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName("username");
        options.setPassword("password".toCharArray());
        options.setCleanSession(false);
        //options.setCleanSession(true);
        //options.setServerURIs(new String[] { "tcp://localhost" });
        options.setServerURIs(new String[] { "url" });
        factory.setConnectionOptions(options);
        return factory;
    }

    @Bean
    public MqttPahoMessageDrivenChannelAdapter inbound() {

        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("admin",
                clientfactory(), "#");
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        adapter.setOutputChannel(mqttInputChannel());
        /*adapter.setc*/
        return adapter;
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttInputChannel")

    public MessageHandler handler() {
        return new MessageHandler() {

            public void handleMessage(Message<?> message) throws MessagingException {

            mqttSubscriptionProcessor.processSubscription(message);


            }

        };
    }

I published a retained message by using this command

mosquitto_pub -u admin -P pwd -t hello/topic  -m "test msg" -r -d

and the result in the eclipse console is

{mqtt_receivedRetained=true, id=48afaec5-debf-4927-ce06-a80556e479ac, mqtt_duplicate=false, mqtt_receivedTopic=hello/topic, mqtt_receivedQos=0, timestamp=1554363853214}
test msg

{mqtt_receivedRetained=true, id=48afaec5-debf-4927-ce06-a80556e479ac, mqtt_duplicate=false, mqtt_receivedTopic=hello/topic, mqtt_receivedQos=0, timestamp=1554363853214}
test msg

{mqtt_receivedRetained=true, id=48afaec5-debf-4927-ce06-a80556e479ac, mqtt_duplicate=false, mqtt_receivedTopic=hello/topic, mqtt_receivedQos=0, timestamp=1554363853214}
test msg 

{mqtt_receivedRetained=true, id=48afaec5-debf-4927-ce06-a80556e479ac, mqtt_duplicate=false, mqtt_receivedTopic=hello/topic, mqtt_receivedQos=0, timestamp=1554363853214}
test msg

Here I need to subscribe the retained topic only once , do have to change any changes in spring integration code.

1

1 Answers

1
votes

This is how retained messages work, the last message published with the retained bit set to a topic will always be delivered first to a client when they subscribe to the matching topic before any new messages.

If you don't want the message to be retained (and as such always delivered) then don't set the retained bit when publishing.

Otherwise as you have discovered you can clear the retained message for a topic by publishing a message with a null payload and the retained bit set to the same topic.

Or you can filter the messages in the client as you can always check the if retained flag is set on the message when it is delivered.

As for the spring side it looks like you are creating 4 clients, so each are receiving the message as they subscribe. You can prove this by looking at the broker logs, if you run mosquitto in verbose mode it will show each message it delivers.