All docs This doc
Skip to end of metadata
Go to start of metadata


This sample demonstrates how WSO2 ESB's MQ Telemetry Transport (MQTT) listener consumes messages from a MQTT topic, and how the MQ Telemetry Transport (MQTT) sender publishes messages to a MQTT topic.


Building the sample

  1. Copy the eclipse Paho MQTT client jar to the <ESB_HOME>/repository/components/lib directory.
  2. Edit the <ESB_HOME>/repository/conf/axis2/axis2.xml file and change the MQTT sender and listener configuration to be as follows:

    <transportReceiver class="org.apache.axis2.transport.mqtt.MqttListener" name="mqtt">
            <parameter locked="false" name="mqttConFactory">
                    <parameter locked="false" name="">localhost</parameter>
                    <parameter locked="false" name="mqtt.server.port">1883</parameter>
                    <parameter locked="false" name="">esb.test.listener</parameter>
                    <parameter locked="false" name="">esb.test2</parameter>
    <transportsender class="org.apache.axis2.transport.mqtt.MqttSender" name="mqtt">
  3. Start the MQTT broker. If you are using WSO2 MB as the MQTT broker, you should set the WSO2 ESB port offset to 1 before running the ESB. To set the port offset in WSO2 ESB, open the <ESB_HOME>/repository/conf/carbon.xml file and set the offset to 1 as follows:

  4. Start WSO2 MB, open the Management Console and create a topic named esb.test2.
  5. Start the ESB with the sample 272 configuration. For instructions on starting a sample ESB configuration, see  Starting the ESB with a sample configurationThe XML configuration for this sample is as follows:

    <definitions xmlns="">
        <proxy name="SampleProxy" transports="mqtt" startOnLoad="true" trace="disable">
                    <address uri="mqtt:/SampleProxy?;mqtt.server.port=1883&amp;;;mqtt.subscription.qos=2&amp;mqtt.blocking.sender=true"/>
                    <property name="OUT_ONLY" value="true"/>
                    <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"/>
            <parameter name="mqtt.connection.factory">mqttConFactory</parameter>
            <parameter name="">esb.test2</parameter>
            <parameter name="mqtt.subscription.qos">2</parameter>
            <parameter name="mqtt.content.type">text/plain</parameter>
            <parameter name="mqtt.session.clean">false</parameter>

    This configuration file synapse_sample_272.xml is available in the <ESB_HOME>/repository/samples directory.

Executing the sample

  • Execute the following command to start the MQTT subscriber on the esb.test2 topic:

    mosquitto_sub -h localhost -t esb.test2
  • Execute the following command to run the MQTT publisher to publish to the esb.test1 topic:

    mosquitto_pub -h localhost -p 1883 -t esb.test1  -m {"company":"WSO2"}

Analyzing the output

When you analyze the output messages on the MQTT subscriber console, you will see the following log:

  • No labels