Last Will and Testament (LWT) is a powerful feature in MQTT that allows clients to specify a message that will be automatically published by the broker on their behalf, if or when an unexpected disconnection occurs.
It provides a reliable means of communication and ensures that clients can gracefully handle disconnections without leaving topics in an inconsistent state. This feature is particularly valuable when clients must notify others of their unavailability or convey important information upon an unexpected disconnection, reference HiveMQ.
Joule supports a consumer LWT handler feature by providing a dedicated DSL attribute, and lwt handler.
Example
In this example, the lwt handler feature is configured for the MQTT consumer.
It specifies that a MQTT publisher disconnects unexpectedly, a message will be sent to the topic sensors/groundfloor/+/status to notify subscribers of the disconnection.
The custom handler will be called when a message has been received on the LWT topic.
This snippet configures the client to process all ground floor room sensors, using the topic wildcard, LWT messages using the SensorOfflineHandler custom class.
Simple implementation of a MQTTLastWillTestamentHandler interface which uses passed in properties that provide a custom log message using the MqttMessage as a parameter.