I'm begininng to use Mqtt and I have a hard time with handling an unreliable network. I'm using a Paho Java Client (in groovy) to publish messages to a distant Mosquitto Broker.
Is there a way, when the broker is unreachable, to have the Paho client persist the message and automatically re-connect to the broker and publish the locally stored messages ? Do I have to handle everything myself, using for example a local broker ?
Here is my client building code
String persistenceDir = config['persistence-dir'] ?: System.getProperty('java.io.tmpdir') def persistence = new MqttDefaultFilePersistence(persistenceDir) client = new MqttAsyncClient(uri, clientId, persistence) client.setCallback(this) options = new MqttConnectOptions() if (config.password) { options.setPassword(config.password as char[]) options.setUserName(config.user) } options.setCleanSession(false) client.connect(options)
And my publish code
def message = new MqttMessage(Json.encode(outgoingMessage).getBytes()) try { client?.connect(options) def topic = client.getTopic('processMsg') message.setQos(1) def token = topic.publish(message) if (client) { client.disconnect() }
Thanks