Camel Paho Connector

Author: Henryk Konsek
3 min read
Camel Paho Connector

Apache Camel 2.16 brings Paho component which provides a connector for the MQTT messaging protocol using the Eclipse Paho library. Paho is one of the most popular MQTT libraries, so if you would like to integrate it with your Java project – Camel Paho connector is a way to go. How can I use

Apache Camel 2.16 brings Paho component which provides a connector for the MQTT messaging protocol using the Eclipse Paho library. Paho is one of the most popular MQTT libraries, so if you would like to integrate it with your Java project – Camel Paho connector is a way to go.

How can I use the Paho connector?

The basic URI format for the Paho connector is as follows:

paho:queueName\[?options\]

The following snippet reads messages from the MQTT broker installed on the same host as the Camel router:

from("paho:some/queue").
  to("mock:test");

This one reads messages from a remote MQTT broker:

from("paho:some/queue?brokerUrl=tcp://iot.eclipse.org:1883").
  to("mock:test");

While this one sends messages to the MQTT broker:

from("direct:test").
  to("paho:some/target/queue");

The complete Spring Boot based application sending message to the MQTT broker every second, is as simple as:

@SpringBootApplication
class MqttRouter extends FatJarRouter {

    @Override
    void configure() {
        from("timer://trigger").
          setBody().expression { randomUUID().toString() }.
          to("paho:topic?brokerUrl={{broker.url}}")
    }

}

Adding the Paho connector to your Maven project

The Paho connector is shipped in a dedicated jar which Maven users should add to their pom.xml file:

<dependency>
    <groupid>org.apache.camel</groupid>
    <artifactid>camel-paho</artifactid>
    <version>2.16.0</version>
</dependency>

Keep in mind that Paho artifacts are not hosted in the Maven Central, so you need to add an Eclipse Paho repository to your POM xml file as well:

<repositories>
  <repository>
    <id>eclipse-paho</id>
    <url>https://repo.eclipse.org/content/repositories/paho-releases</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>

A default payload type

By default the Camel Paho component operates on the binary payloads extracted out of (or put into) the MQTT message:

// Receive payload
byte\[\] payload = (byte\[\]) consumerTemplate.receiveBody("paho:topic");

// Send payload
byte\[\] payload = "message".getBytes();
producerTemplate.sendBody("paho:topic", payload);

Still Camel build-in type conversion API can perform automatic data type transformations for you. In the example below Camel automatically converts binary payload into String (and conversely):

// Receive payload
String payload = consumerTemplate.receiveBody("paho:topic", String.class);

// Send payload
String payload = "message";
producerTemplate.sendBody("paho:topic", payload);

Connection options

The convention-over-configuration approach used in Camel is really handy for most of the situations, yet sometimes you may need a more fine-grained control over an MQTT client connection. To account for such situations just add the bean of the type org.eclipse.paho.client.mqttv3.MqttConnectOptions to your Camel registry. For Spring applications it will mean adding the bean to your application context. The snippet below uses password-based authentication to connect to the MQTT broker:

@Bean
MqttConnectOptions connectOptions() {
  MqttConnectOptions connectOptions = new MqttConnectOptions();
  connectOptions.setUserName("henry");
  connectOptions.setPassword("secret".toCharArray());
  return connectOptions;
}

That’s it. Camel automatically picks up this MqttConnectOptions bean from the registry and uses it to establish connection with the MQTT broker.

What’s next?

If you are interested in all the available options of the Camel Paho connector, visit the component page. If you are looking for a complete example of the project that can easily be deployed into micro-computers like Raspberry Pi check out CamelM2M MQTT quickstart. If you wonder how fast MQTT connector works – check out my previous blog post related to the MQTT performance on Raspberry Pi.