22. MQTT Support

22.1 Introduction

Spring Integration provides inbound and outbound channel adapters supporting the MQ Telemetry Transport (MQTT) protocol. The current implementation uses the Eclipse Paho MQTT Client library.

Configuration of both adapters is achieved using the DefaultMqttPahoClientFactory. Refer to the Paho documentation for more information about configuration options.

22.2 Inbound (message-driven) Channel Adapter

The inbound channel adapter is implemented by the MqttPahoMessageDrivenChannelAdapter. For convenience, it can be configured using the namespace. A minimal configuration might be:

<bean id="clientFactory"
		class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
	<property name="userName" value="${mqtt.username}"/>
	<property name="password" value="${mqtt.password}"/>
</bean>

<int-mqtt:message-driven-channel-adapter id="mqttInbound"
	client-id="${mqtt.default.client.id}.src"
	url="${mqtt.url}"
	topics="sometopic"
	client-factory="clientFactory"
	channel="output"/>

Attributes:

<int-mqtt:message-driven-channel-adapter id="oneTopicAdapter"
	client-id="foo" 1
	url="tcp://localhost:1883" 2
	topics="bar" 3
	converter="myConverter" 4
	client-factory="clientFactory" 5
	send-timeout="123" 6
	error-channel="errors" 7
	channel="out" />

1

The client id.

2

The broker URL.

3

A comma delimited list of topics from which this adapter will receive messages.

4

An MqttMessageConverter (optional). The default DefaultPahoMessageConverter produces a message with a String payload (by default) with the following headers:
  • mqtt_topic - the topic from which the message was received
  • mqtt_duplicate - true if the message is a duplicate
  • mqtt_qos - the quality of service
The DefaultPahoMessageConverter can be configured to return the raw byte[] in the payload by declaring it as a <bean/> and setting the payloadAsBytes property.

5

The client factory.

6

The send timeout - only applies if the channel might block (such as a bounded QueueChannel that is currently full).

7

The error channel - downstream exceptions will be sent to this channel, if supplied, in an ErrorMessage; the payload is a MessagingException containing the failed message and cause.

22.3 Outbound Channel Adapter

The outbound channel adapter is implemented by the MqttPahoMessageHandler which is wrapped in a ConsumerEndpoint. For convenience, it can be configured using the namespace.

Attributes:

<int-mqtt:outbound-channel-adapter id="withConverter"
	client-id="foo" 1
	url="tcp://localhost:1883" 2
	converter="myConverter" 3
	client-factory="clientFactory" 4
	default-qos="1" 5
	default-retained="true" 6
	default-topic="bar" 7
	channel="target" />

1

The client id.

2

The broker URL.

3

An MqttMessageConverter (optional). The default DefaultPahoMessageConverter recognizes the following headers:
  • mqtt_topic - the topic to which the message will be sent
  • mqtt_retained - true if the message is to be retained
  • mqtt_qos - the quality of service

4

The client factory.

5

The default quality of service (used if no mqtt_qos header is found). Not allowed if a custom converter is supplied.

6

The default value of the retained flag (used if no mqtt_retaind header is found). Not allowed if a custom converter is supplied.

7

The default topic to which the message will be sent (used if no mqtt_topic header is found).