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.