이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 123. Paho


Paho Component

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

paho:queueName[?options]
Copy to Clipboard Toggle word wrap
For example 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");
Copy to Clipboard Toggle word wrap
While the snippet below sends message to the MQTT broker:
from("direct:test").
  to("paho:some/target/queue");
Copy to Clipboard Toggle word wrap
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:
from("paho:some/queue?brokerUrl=tcp://iot.eclipse.org:1883").
  to("mock:test");
Copy to Clipboard Toggle word wrap

Adding the component to the project

Maven users will need to add the following dependency to their pom.xml for this component:
<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>
Copy to Clipboard Toggle word wrap
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:
<repositories>
  <repository>
    <id>eclipse-paho</id>
    <url>https://repo.eclipse.org/content/repositories/paho-releases</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>
Copy to Clipboard Toggle word wrap

Default payload type

By default 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);
Copy to Clipboard Toggle word wrap
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):
// Receive payload
String payload = consumerTemplate.receiveBody("paho:topic", String.class);
 
// Send payload
String payload = "message";
producerTemplate.sendBody("paho:topic", payload);
Copy to Clipboard Toggle word wrap

URI Options

Expand
Option
Default
Description
clientId camel-<timestamp> MQTT client identifier.
brokerUrl
tcp://localhost:1883
The URL of the MQTT broker.
persistence memory Client persistence to be used - memory or file.
filePersistenceDirectory current directory (Camel 2.16.1 and 2.17) Base directory used by file persistence. Takes no effect if non-file persistence is used.
qos 2 Client quality of service level (0-2).
connectOptions none The 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:
@Bean
MqttConnectOptions connectOptions() {
  MqttConnectOptions connectOptions = new MqttConnectOptions();
  connectOptions.setUserName("henry");
  connectOptions.setPassword("secret".toCharArray());
  return connectOptions;
}
Copy to Clipboard Toggle word wrap

Headers

The following headers are recognized by the Paho component:
Expand
Header
Java constant
Endpoint typeValue type
Description
PahoOriginalMessage PahoConstants.HEADER_ORIGINAL_MESSAGE Consumer org.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.
CamelMqttTopic PahoConstants.MQTT_TOPIC Consumer String Camel 2.17: The topic
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat