Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Paho component provides 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.

URI format

Code Block
paho:queueName[?options]

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

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

While the snippet below sends message to the MQTT broker:

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

You can append query options to the URI in the following format: ?option=value&option=value&... . For example this is how to read messages from the remote MQTT broker: 

 

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

 

Adding the component to the project

...

Code Block
xml
xml
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-paho</artifactId>
    <version>x.y.z</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

Code Block
paho:queueName[?options]

You can append query options to the URI in the following format: ?option=value&option=value&... .

URI Options

Div
classconfluenceTableSmall

Option

Default

Description

brokerUrl

tcp://localhost:1883

The URL of the MQTT broker.

persistencememoryClient persistence to be used - memory or file.
qos2Client quality of service level (0-2).

Example

Assumed we have a java bean with the following annotations

Code Block
titleCar.java
public class Car {

    @NotNull
    private String manufacturer;

    @NotNull
    @Size(min = 5, max = 14, groups = OptionalChecks.class)
    private String licensePlate;
    
    // getter and setter
}

and an interface definition for our custom validation group

Code Block
titleOptionalChecks.java
public interface OptionalChecks {
}

with the following Camel route, only the @NotNull constraints on the attributes manufacturer and licensePlate will be validated (Camel uses the default group javax.validation.groups.Default).

Code Block
from("direct:start")
.to("bean-validator://x")
.to("mock:end")

If you want to check the constraints from the group OptionalChecks, you have to define the route like this

Code Block
from("direct:start")
.to("bean-validator://x?group=OptionalChecks")
.to("mock:end")

If you want to check the constraints from both groups, you have to define a new interface first

Code Block
titleAllChecks.java
@GroupSequence({Default.class, OptionalChecks.class})
public interface AllChecks {
}

and then your route definition should looks like this

Code Block
from("direct:start")
.to("bean-validator://x?group=AllChecks")
.to("mock:end")

And if you have to provide your own message interpolator, traversable resolver and constraint validator factory, you have to write a route like this

Code Block
<bean id="myMessageInterpolator" class="my.ConstraintValidatorFactory" />
<bean id="myTraversableResolver" class="my.TraversableResolver" />
<bean id="myConstraintValidatorFactory" class="my.ConstraintValidatorFactory" />

from("direct:start")
.to("bean-validator://x?group=AllChecks&messageInterpolator=#myMessageInterpolator
&traversableResolver=#myTraversableResolver&constraintValidatorFactory=#myConstraintValidatorFactory")
.to("mock:end")

It's also possible to describe your constraints as XML and not as Java annotations. In this case, you have to provide the file META-INF/validation.xml which could looks like this

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

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

Default payload type

 

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

Code Block
// Receive payload
byte[] payload = (byte[]) consumerTemplate.receiveBody("paho:topic");
 
// Send payload
byte[] payload = "message".getBytes();
producerTemplate.sendBody("paho:topic", payload);

 

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

 

Code Block
// Receive payload
String payload = consumerTemplate.receiveBody("paho:topic", String.class);
 
// Send payload
String payload = "message";
producerTemplate.sendBody("paho:topic", payload);


URI Options

Div
classconfluenceTableSmall

Option

Default

Description

clientIdcamel-<timestamp>MQTT client identifier.

brokerUrl

tcp://localhost:1883

The URL of the MQTT broker.

persistencememoryClient persistence to be used - memory or file.
filePersistenceDirectorycurrent directory(Camel 2.16.1 and 2.17) Base directory used by file persistence. Takes no effect if non-file persistence is used.
qos2Client quality of service level (0-2).
connectOptionsnoneThe reference to the org.eclipse.paho.client.mqttv3.MqttConnectOptions instance located in the Camel registry. Referenced MqttConnectOptions instance will be used by the endpoint to initialize the connection. For example connectOptions=#myConnectOptions notation can be used to reference Spring bean named myConnectOptions. If there is only a single instance of the MqttConnectOptions in the registry, it will be automatically picked up by the endpoint.

For example the convention-over-configuration approach used in Camel is really handy for the most of the situations, but sometimes you would like to have more fine-grained control over the MQTT client connection. To cover such situations just add the bean of type org.eclipse.paho.client.mqttv3.MqttConnectOptions to your Camel registry. For Spring applications that would means adding bean to your application context. The snippet below uses password-based authentication to connect to the MQTT broker:

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


Headers

The following headers are recognized by the Paho component:

Div
classconfluenceTableSmall

Header

Java constant

Endpoint typeValue type

Description

PahoOriginalMessagePahoConstants.HEADER_ORIGINAL_MESSAGEConsumerorg.eclipse.paho.client.mqttv3.MqttMessage

The original Paho message instance received by the client.

Deprecated: from Camel 2.17 onwards the original MqttMessage is not stored as a header but on the org.apache.camel.component.paho.PahoMessage message that has a getter getMqttMessage.

CamelMqttTopicPahoConstants.MQTT_TOPICConsumerStringCamel 2.17:The topic

 and the constraints-car.xml file

Include Page
Endpoint See Also
Endpoint See Also