I'm trying to implement eclipse.paho
in my project to connect Mqtt Broker (Both subscribing and publishing purpose). The problem is, when I using the subscribing feature (Implementing MqttCallback
interface), I couldn't figure our how can I reconnect if the connection lost. MqttCallback interface has a connectionLost method, but it is useful for the debug what causes the connection lost. I searched but couldn't find a way to establish auto reconnect. Can you suggest a way or document about this problem?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
To use auto reconnect, just set
setAutomaticReconnect(true)
on theMqttConnectOptions
object.The best way to do this is to structure your connection logic so it lives in a method on it's own so it can be called again from the
connectionLost
callback in theMqttCallback
instance.The
connectionLost
method is passed a Throwable that will be the exception that triggered the disconnect so you can make decisions about the root cause and how this may effect when/how you reconnect.The connection method should connect and subscribe to the topics you require.
Something like this: