Skip to content

Commit

Permalink
Dispose mqtt client on application close
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengees committed Jul 5, 2021
1 parent 9aa7ea4 commit c856f89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c856f89

Please sign in to comment.