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/.

Simple MQTT Client

This sample demonstrates how to send and receive messages in WSO2 Message broker via the MQTT transport.

About the sample

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

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 SimpleMqttSample 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 SimpleMqttSample sample folder in the <MB_HOME>/samples directory.

  2. Execute the ant command to build and run the SimpleMqttSample sample.

  3. If the build is successful, the output will be printed in the terminal as shown below.

Run the ant or the mvn clean install command from the <MB_HOME>/samples/SimpleMQTTClient directory. 

Analyzing the output

When you run this sample, the following will be displayed in the output log of the console depending on the command you used:

For the ant command:

[java] INFO  [org.wso2.sample.mqtt.Main] - Running sample
[java] INFO  [org.wso2.sample.mqtt.SimpleMQTTCallback] - Message delivered successfully to topic : "simpleTopic".
[java] INFO  [org.wso2.sample.mqtt.Main] - Clients Disconnected!

For the maven clean install command:

INFO  [org.wso2.sample.mqtt.Main] - Running sample
INFO  [org.wso2.sample.mqtt.SimpleMQTTCallback] - Message delivered successfully to topic : "simpleTopic".
INFO  [org.wso2.sample.mqtt.Main] - Clients Disconnected!