GH-9597: Fix Mqttv5PahoMessageHandler.publish() for concurrency

Fixes: #9597
Issue link: https://github.com/spring-projects/spring-integration/issues/9597

When input channel for the endpoint with a `Mqttv5PahoMessageHandler` is an `ExecutorChannel` and a lot of concurrent messages are sent,
the `Connect already in progress (32110)` exception can be thrown

* Wrap `this.mqttClient.connect()` in the `Mqttv5PahoMessageHandler.publish()` into a `Lock`

(cherry picked from commit 334620f36e)
This commit is contained in:
Artem Bilan
2024-10-25 16:52:27 -04:00
committed by Spring Builds
parent 44d035fcaa
commit fb6e6c63df

View File

@@ -266,7 +266,15 @@ public class Mqttv5PahoMessageHandler extends AbstractMqttMessageHandler<IMqttAs
long completionTimeout = getCompletionTimeout();
try {
if (!this.mqttClient.isConnected()) {
this.mqttClient.connect(this.connectionOptions).waitForCompletion(completionTimeout);
this.lock.lock();
try {
if (!this.mqttClient.isConnected()) {
this.mqttClient.connect(this.connectionOptions).waitForCompletion(completionTimeout);
}
}
finally {
this.lock.unlock();
}
}
IMqttToken token = this.mqttClient.publish(topic, (MqttMessage) mqttMessage);
ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher();