MQTT Component

Table of Contents

URI format
Options
Samples
Endpoints
See Also

Available as of Camel 2.10

The mqtt: component is used for communicating with MQTT compliant message brokers, like Apache ActiveMQ or Mosquitto

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-mqtt</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

mqtt://name[?options]

Where name is the name you want to assign the component.

Options

The MQTT component supports 3 options which are listed below.

{% raw %}

NameJava TypeDescription

host

String

The URI of the MQTT broker to connect too - this component also supports SSL - e.g. ssl://127.0.0.1:8883

userName

String

Username to be used for authentication against the MQTT broker

password

String

Password to be used for authentication against the MQTT broker

{% endraw %}

The MQTT component supports 39 endpoint options which are listed below:

{% raw %}

NameGroupDefaultJava TypeDescription

name

common

 

String

Required A logical name to use which is not the topic name.

blockingExecutor

common

 

Executor

SSL connections perform blocking operations against internal thread pool unless you call the setBlockingExecutor method to configure that executor they will use instead.

byDefaultRetain

common

false

boolean

The default retain policy to be used on messages sent to the MQTT broker

cleanSession

common

false

boolean

Set to false if you want the MQTT server to persist topic subscriptions and ack positions across client sessions. Defaults to true.

clientId

common

 

String

Use to set the client Id of the session. This is what an MQTT server uses to identify a session where setCleanSession(false); is being used. The id must be 23 characters or less. Defaults to auto generated id (based on your socket address port and timestamp).

connectAttemptsMax

common

-1

long

The maximum number of reconnect attempts before an error is reported back to the client on the first attempt by the client to connect to a server. Set to -1 to use unlimited attempts. Defaults to -1.

connectWaitInSeconds

common

10

int

Delay in seconds the Component will wait for a connection to be established to the MQTT broker

disconnectWaitInSeconds

common

5

int

The number of seconds the Component will wait for a valid disconnect on stop() from the MQTT broker

dispatchQueue

common

 

DispatchQueue

A HawtDispatch dispatch queue is used to synchronize access to the connection. If an explicit queue is not configured via the setDispatchQueue method then a new queue will be created for the connection. Setting an explicit queue might be handy if you want multiple connection to share the same queue for synchronization.

host

common

tcp://127.0.0.1:1883

URI

The URI of the MQTT broker to connect too - this component also supports SSL - e.g. ssl://127.0.0.1:8883

keepAlive

common

 

short

Configures the Keep Alive timer in seconds. Defines the maximum time interval between messages received from a client. It enables the server to detect that the network connection to a client has dropped without having to wait for the long TCP/IP timeout.

localAddress

common

 

URI

The local InetAddress and port to use

maxReadRate

common

 

int

Sets the maximum bytes per second that this transport will receive data at. This setting throttles reads so that the rate is not exceeded. Defaults to 0 which disables throttling.

maxWriteRate

common

 

int

Sets the maximum bytes per second that this transport will send data at. This setting throttles writes so that the rate is not exceeded. Defaults to 0 which disables throttling.

mqttQosPropertyName

common

MQTTQos

String

The property name to look for on an Exchange for an individual published message. If this is set (one of AtMostOnce AtLeastOnce or ExactlyOnce ) - then that QoS will be set on the message sent to the MQTT message broker.

mqttRetainPropertyName

common

MQTTRetain

String

The property name to look for on an Exchange for an individual published message. If this is set (expects a Boolean value) - then the retain property will be set on the message sent to the MQTT message broker.

mqttTopicPropertyName

common

MQTTTopicPropertyName

String

These a properties that are looked for in an Exchange - to publish to

publishTopicName

common

camel/mqtt/test

String

The default Topic to publish messages on

qualityOfService

common

AtLeastOnce

String

Quality of service level to use for topics.

receiveBufferSize

common

65536

int

Sets the size of the internal socket receive buffer. Defaults to 65536 (64k)

reconnectAttemptsMax

common

-1

long

The maximum number of reconnect attempts before an error is reported back to the client after a server connection had previously been established. Set to -1 to use unlimited attempts. Defaults to -1.

reconnectBackOffMultiplier

common

2.0

double

The Exponential backoff be used between reconnect attempts. Set to 1 to disable exponential backoff. Defaults to 2.

reconnectDelay

common

10

long

How long to wait in ms before the first reconnect attempt. Defaults to 10.

reconnectDelayMax

common

30000

long

The maximum amount of time in ms to wait between reconnect attempts. Defaults to 30000.

sendBufferSize

common

65536

int

Sets the size of the internal socket send buffer. Defaults to 65536 (64k)

sendWaitInSeconds

common

5

int

The maximum time the Component will wait for a receipt from the MQTT broker to acknowledge a published message before throwing an exception

sslContext

common

 

SSLContext

To configure security using SSLContext configuration

subscribeTopicName

common

 

String

These are set on the Endpoint - together with properties inherited from MQTT

subscribeTopicNames

common

 

String

A comma-delimited list of Topics to subscribe to for messages. Note that each item of this list can contain MQTT wildcards ( and/or ) in order to subscribe to topics matching a certain pattern within a hierarchy. For example is a wildcard for all topics at a level within the hierarchy so if a broker has topics topics/one and topics/two then topics/ can be used to subscribe to both. A caveat to consider here is that if the broker adds topics/three the route would also begin to receive messages from that topic.

trafficClass

common

8

int

Sets traffic class or type-of-service octet in the IP header for packets sent from the transport. Defaults to 8 which means the traffic should be optimized for throughput.

version

common

3.1

String

Set to 3.1.1 to use MQTT version 3.1.1. Otherwise defaults to the 3.1 protocol version.

willMessage

common

 

String

The Will message to send. Defaults to a zero length message.

willQos

common

AtMostOnce

QoS

Sets the quality of service to use for the Will message. Defaults to AT_MOST_ONCE.

willRetain

common

 

QoS

Set to true if you want the Will to be published with the retain option.

willTopic

common

 

String

If set the server will publish the client’s Will message to the specified topics if the client has an unexpected disconnection.

bridgeErrorHandler

consumer

false

boolean

Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN/ERROR level and ignored.

exceptionHandler

consumer (advanced)

 

ExceptionHandler

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN/ERROR level and ignored.

exchangePattern

consumer (advanced)

 

ExchangePattern

Sets the exchange pattern when the consumer creates an exchange.

synchronous

advanced

false

boolean

Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported).

{% endraw %}

Samples

Sending messages:

from("direct:foo").to("mqtt:cheese?publishTopicName=test.mqtt.topic");

Consuming messages:

from("mqtt:bar?subscribeTopicName=test.mqtt.topic").transform(body().convertToString()).to("mock:result")

Endpoints

Camel supports the Message Endpoint pattern using the Endpoint interface. Endpoints are usually created by a Component and Endpoints are usually referred to in the DSL via their URIs.

From an Endpoint you can use the following methods

See Also