Configure the Last Will And Testament Messages for MQTT Connector

In this example, you set a last will and testament (LWT) message for the Anypoint Connector for MQTT (MQTT Connector) to ensure that if the client disconnects temporarily or permanently from the broker, a message is sent to a specific LWT topic, notifying all its subscribers of the client’s disconnection. The LWT message is set at the time the connection to the broker is established.

Configure the LWT message in the configuration element and specify whether or not you want the broker to retain that message. If an MQTT message is set for retention, the broker stores the last message marked as retained that arrived at a specific topic. If no listeners are subscribed to a matching topic at the time the message is sent, the broker stores the message until a listener connects to that topic. Then, the broker delivers the listener to the last message retained for that topic.

  1. In Studio, navigate to the Global Elements tab.

  2. Click Create.

  3. In the filter box, type mqtt and select MQTT3 Config.

  4. Click OK.

  5. In the MQTT3 Config window, for Connection, select MQTT3 Form Connection.

  6. For Client id generator, select Client id random suffix generator.

  7. Set the following fields:

    • Client ID: smart-bentley-123

    • Username: usertest

    • Password: passtest

    • URL: tcp://127.0.0.1:1883

  8. Click LWT tab.

  9. Set the following fields:

    • Topic: LWT

    • Body: Testing body message

    • Is retained: True.

  10. Click OK.

MQTT Config window with Last Will and Testament configuration window selected

In the Configuration XML editor, the <mqtt3:last-will-and-testament> configuration looks like this:

<mqtt3:config name="MQTT_LWT_Config">
    <mqtt3:connection username="usertest" password="passtest" url="tcp://127.0.0.1:1883" >
        <mqtt3:client-id-generator>
            <mqtt3:client-id-random-suffix-generator clientId="smart-bentley-123" />
        </mqtt3:client-id-generator>
        <mqtt3:last-will-and-testament topic="LWT" body="Testing body message" isRetained="true"/>
    </mqtt3:connection>
</mqtt3:config>