Spring integration 什么';当令牌现在已过期时,处理与MQTTPAHomeMessageHandler重新连接的最佳方法是什么?

Spring integration 什么';当令牌现在已过期时,处理与MQTTPAHomeMessageHandler重新连接的最佳方法是什么?,spring-integration,mqtt,spring-integration-mqtt,Spring Integration,Mqtt,Spring Integration Mqtt,我使用以下示例作为我自己的代码发布到MQTT服务器的基础: 我有一个特殊的用例,其中密码是一个令牌,特别是一个将过期的keydepose令牌。如果出于任何原因,spring应用程序与MQTT服务器失去连接并尝试重新连接,则令牌将已过期,并将引发MqttSecurityException:Not authorized to connect异常。我尝试在MqttPahoMessageHandler中扩展方法connectionLost,但由于MqttPahoClientFactory和IMqttAs

我使用以下示例作为我自己的代码发布到MQTT服务器的基础:

我有一个特殊的用例,其中密码是一个令牌,特别是一个将过期的keydepose令牌。如果出于任何原因,spring应用程序与MQTT服务器失去连接并尝试重新连接,则令牌将已过期,并将引发MqttSecurityException:Not authorized to connect异常。我尝试在MqttPahoMessageHandler中扩展方法connectionLost,但由于MqttPahoClientFactory和IMqttAsyncClient是私有final,因此我无能为力。想知道是否有其他我没有想到的方法,或者这个库不打算这样使用


感谢您的回复。

我们从中获得了
MqttConnectOptions
,因此您应该可以在那里更新密码

如果出于某种原因,这不起作用,请打开一个

编辑

关于你的评论,这有什么不对

@Bean
公共MqttPahoClientFactory mqttClientFactory(){
DefaultMqttPahoClientFactory=新的DefaultMqttPahoClientFactory();
MqttConnectOptions=new MqttConnectOptions();
options.setServerURI(新字符串[]{”tcp://localhost:1883" });
选项。设置用户名(“来宾”);
options.setPassword(“guest.tocharray());
工厂设置连接选项(选项);
返回工厂;
}
@豆子
公共应用程序运行程序运行程序(MqttPahoClientFactory mqttClientFactory、MqttPahoMessageHandler){
返回参数->{
睡眠(30_000);
System.out.println(“更改密码”);
mqttClientFactory.getConnectionOptions().setPassword(“foo.toCharray());
handler.stop();
handler.start();
};
}

谢谢,问题是因为我使用DefaultMqttPahoClientFactory作为bean。一旦在bean中设置了密码,就似乎没有地方可以在连接丢失时更新MqttConnectOptions中的密码。我将提出一个功能请求……我不明白——它对我来说很好;请参阅对我的答案的编辑。你的意思是你不知道什么时候连接断了?如果您还有入站适配器,则可以侦听
MqttConnectionFailedEvent
;当生产者端的连接丢失时,我们可能也应该发布这样的事件。是的,您提出的解决方案并不能真正解决我的问题。我希望能够检索新令牌,并在应用程序与MQTT之间的连接丢失时重新连接到MQTT服务器。我为MqttConnectionFailedEvent添加了一个侦听器&创建了自己的MQTTPAHomeMessageHandler实现,将PublisheEvent代码添加到connectionLost中,如MQTTPAHomeMessageDrivenchannelAdapter中所示。然后侦听器只检索一个新令牌并重新启动MessageHandler;请考虑为事件出版物提交公关。
foo
2020-03-10 17:42:33.560  INFO 95638 --- [iSampleConsumer] siSample                                 
  : foo sent to MQTT, received from MQTT
Changing password
foo
2020-03-10 17:43:08.705 ERROR 95638 --- [ask-scheduler-3] o.s.integration.handler.LoggingHandler   
  : org.springframework.messaging.MessageHandlingException: error occurred in message handler [bean 'mqttOutbound' for component 'mqttOutFlow.org.springframework.integration.config.ConsumerEndpointFactoryBean#1'; defined in: 'com.example.demo.So60610337Application'; from source: 'org.springframework.core.type.StandardMethodMetadata@79da8dc5']; nested exception is org.springframework.messaging.MessagingException: Failed to connect; nested exception is Bad user name or password (4), failedMessage=GenericMessage [payload=foo sent to MQTT, headers={id=4eab5b52-726f-7ea3-252d-77c4d0401cc8, timestamp=1583876588662}]
...
Caused by: Bad user name or password (4)