This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

MQTT Retain

This sample demonstrates how WSO2 MB handles message arrivals, delivery completions and connections lost when the MQTT transport is used.

About the sample

The <MB_HOME>/Samples/MqttRetainSample/src/main/java/org/wso2/sample/mqtt directory has the following classes:

Prerequisites

Before you build the sample, the prerequisites for MB samples should be in place.

Building the sample

If you are building an MQTT sample for the first time, you need to build the sample using Maven. This will download the Maven dependencies needed for your MQTT samples. Once the dependencies are downloaded, you can build any of the MQTT samples using either Maven or Ant.

Using Maven:
  1. Navigate to the MqttRetainSample sample folder in the <MB_HOME>/samples directory.
  2. Execute the mvn clean install command to build and run the sample.
  3. If the build is successful, the output will be printed in the terminal as shown below.
Using Ant:

Be sure that the Maven dependencies required for MQTT samples are already downloaded as explained here.

  1. Navigate to the MqttRetainSample sample folder in the <MB_HOME>/samples directory.

  2. Execute the ant command to build and run the mqtt retain sample.

  3. Once the sample is started, you will be asked to enter a topic name to publish/subscribe. Enter an appropriate name and proceed.

  4. Then it will ask if the retain feature should be enabled for the given topic. Enter Y to enable the retain feature. You can check the behaviour before the retain feature by entering N as the value.

  5. If all parameters are set correctly enter Y and proceed. Or else, you can re-enter values by type N.
  6. If the build is successful, the output will be printed in the terminal as shown below.

Analyzing the output

Following is the output is printed in the terminal if the retain message is successfully received for future subscriber:

run:
     [java] INFO  [org.wso2.sample.mqtt.Main] - Retain Topic Sample
     [java] INFO  [org.wso2.sample.mqtt.Main] - Enter topic name : new1
     [java] INFO  [org.wso2.sample.mqtt.Main] - Set retain enable [Y/N] : Y
     [java] INFO  [org.wso2.sample.mqtt.Main] - 
     [java] Enter Y to continue with new1 topic name and retain state true.
     [java] Enter N to revise parameters [Y/N] : Y
     
     [java] INFO  [org.wso2.sample.mqtt.Main] - Start sample with Topic Name new1 with retain true.
     [java] INFO  [org.wso2.sample.mqtt.Main] - Publish topic message with retain enabled for topic name new1
     [java] INFO  [org.wso2.sample.mqtt.SimpleMQTTCallback] - Message delivered successfully to topic : "new1".
     [java] INFO  [org.wso2.sample.mqtt.Main] - Subscribe for topic name new1
     [java] INFO  [org.wso2.sample.mqtt.SimpleMQTTCallback] - Message arrived on 
     topic : "new1" Message : "sample message payload"
     
     [java] INFO  [org.wso2.sample.mqtt.Main] - Clients Disconnected!