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/.
Sample 272:Publishing and Subscribing using WSO2 ESB's MQ Telemetry Transport
Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation :
<PRODUCT_HOME>/
repository/samples/
directory that includes all Integration profile samples is changed to<EI_HOME>/
samples/service-bus/
.<PRODUCT_HOME>/
repository/samples/resources/
directory that includes all artifacts related to the Integration profile samples is changed to<EI_HOME>/
samples/service-bus/resources/
.
Introduction
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.
Prerequisites
Download theÂ
org.eclipse.paho.client.mqttv3-1.1.0.jar
 file.Download the Mosquitto-clients (http://mosquitto.org/)
- Download WSO2 MB 3.1.0 or the mosquitto MQTT broker (http://mosquitto.org/)
Building the sample
- Copy the
org.eclipse.paho.client.mqttv3-1.1.0.jar
 file to the<EI_HOME>/lib
directory. Edit theÂ
<EI_HOME>/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="mqtt.server.host.name">localhost</parameter> <parameter locked="false" name="mqtt.server.port">1883</parameter> <parameter locked="false" name="mqtt.client.id">esb.test.listener</parameter> <parameter locked="false" name="mqtt.topic.name">esb.test1</parameter> </parameter> </transportReceiver> <transportsender class="org.apache.axis2.transport.mqtt.MqttSender" name="mqtt"> </transportsender>
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
<EI_HOME>/conf/carbon.xml
file and set the offset to 1 as follows:<Offset>1</Offset>
- Start WSO2 MB, open the Management Console and create a topic named esb.test2.
Start the ESB with the sample 272 configuration. For instructions on starting a sample ESB configuration, see Starting the ESB with a sample configuration. The XML configuration for this sample is as follows:
<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="SampleProxy" transports="mqtt" startOnLoad="true" trace="disable"> <description/> <target> <endpoint> <address uri="mqtt:/SampleProxy?mqtt.server.host.name=localhost&mqtt.server.port=1883&mqtt.client.id=esb.test.sender&mqtt.topic.name=esb.test2&mqtt.subscription.qos=2&mqtt.blocking.sender=true"/> </endpoint> <inSequence> <property name="OUT_ONLY" value="true"/> <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"/> </inSequence> <outSequence> <send/> </outSequence> </target> <parameter name="mqtt.connection.factory">mqttConFactory</parameter> <parameter name="mqtt.topic.name">esb.test1</parameter> <parameter name="mqtt.subscription.qos">2</parameter> <parameter name="mqtt.content.type">text/plain</parameter> <parameter name="mqtt.session.clean">false</parameter> </proxy> </definitions> Â
This configuration file
synapse_sample_272.xmlÂ
is available in theÂ<EI_HOME>/samples/service-bus
 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:
{"company":"WSO2"}