【发布时间】:2019-04-04 07:46:00
【问题描述】:
订阅保留主题时获取重复的保留消息。
我在我的物联网项目中使用了 spring mqtt 集成。 在这里,一旦收到保留的消息,它就会继续订阅,直到我将空白消息发布到同一主题,并将保留标志设置为 true。 我注意到当我在终端中使用 mqtt 命令执行相同的过程时,订阅保留的主题时,它只订阅一次,不会发生重复订阅。
我使用下面的代码通过#订阅所有主题
@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);
}
};
}
我使用此命令发布了保留消息
mosquitto_pub -u admin -P pwd -t hello/topic -m "test msg" -r -d
在eclipse控制台中的结果是
{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
这里我只需要订阅一次保留的主题,必须更改spring集成代码中的任何更改。
【问题讨论】:
标签: java spring spring-integration mqtt mosquitto