Last will and testament publication
If an MQTT client connection unexpectedly ends, we can configure MQ Telemetry to send a "last will and testament" publication. Predefine the content of the publication, and the topic to send it to. The "last will and testament" is a connection property. Create it before connecting the client.
Create a topic for the last will and testament. We might create a topic such as MQTTManagement/Connections/server URI/client identifer/Lost.
Set up a "last will and testament" using the MqttConnectionOptions.setWill(MqttTopic lastWillTopic, byte [] lastWillPayload, int lastWillQos, boolean lastWillRetained) method.
Consider creating a time stamp in the lastWillPayload message. Include other client information that assists in identifying the client and the circumstances of the connection. Pass the MqttConnectionOptions object to the MqttClient constructor.
Set lastWillQos to 1 or 2, to make the message persistent in IBM MQ, and to guarantee delivery. To retain the last lost connection information, set the lastWillRetained to true.
The "last will and testament" publication is sent to subscribers if the connection ends unexpectedly. It is sent if the connection ends without the client calling the MqttClient.disconnect method.
To monitor connections, complement the "last will and testament" publication with other publications to record connections and programmed disconnections.
Parent topic: MQTT client programming concepts
Related concepts
- Callbacks and synchronization in MQTT client applications
- Clean sessions
- Client identifier
- Delivery tokens
- Message persistence in MQTT clients
- Publications
- Qualities of service provided by an MQTT client
- Retained publications and MQTT clients
- Subscriptions
- Topic strings and topic filters in MQTT clients