diff --git a/src/main/kotlin/de/smartsquare/starter/mqtt/DisposableMqtt3Client.kt b/src/main/kotlin/de/smartsquare/starter/mqtt/DisposableMqtt3Client.kt new file mode 100644 index 0000000..fee248b --- /dev/null +++ b/src/main/kotlin/de/smartsquare/starter/mqtt/DisposableMqtt3Client.kt @@ -0,0 +1,12 @@ +package de.smartsquare.starter.mqtt + +import com.hivemq.client.mqtt.mqtt3.Mqtt3Client +import org.springframework.beans.factory.DisposableBean +import java.util.concurrent.TimeUnit + +class DisposableMqtt3Client(private val delegate: Mqtt3Client) : Mqtt3Client by delegate, DisposableBean { + + override fun destroy() { + delegate.toAsync().disconnect().get(10, TimeUnit.SECONDS) + } +} diff --git a/src/main/kotlin/de/smartsquare/starter/mqtt/MqttAutoConfiguration.kt b/src/main/kotlin/de/smartsquare/starter/mqtt/MqttAutoConfiguration.kt index 0682bbb..db64edd 100644 --- a/src/main/kotlin/de/smartsquare/starter/mqtt/MqttAutoConfiguration.kt +++ b/src/main/kotlin/de/smartsquare/starter/mqtt/MqttAutoConfiguration.kt @@ -58,7 +58,7 @@ class MqttAutoConfiguration { } else { logger.info("Successfully connected to broker.") - return client + return DisposableMqtt3Client(client) } } catch (e: TimeoutException) { throw BrokerConnectException("Broker ${config.host}:${config.port} did not respond within 10 seconds.", e)