RocketMQ
Since Camel 3.20
Both producer and consumer are supported
The RocketMQ component allows you to produce and consume messages from RocketMQ instances.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rocketmq</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> | Since RocketMQ 5.x API is compatible with 4.x, this component works with both RocketMQ 4.x and 5.x. Users could change RocketMQ dependencies on their own. |
URI format
rocketmq:topicName?[options]
The topic name determines the topic to which the produced messages will be sent to. In the case of consumers, the topic name determines the topic will be subscribed. This component uses RocketMQ push consumer by default.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The RocketMQ component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The access channel of the RocketMQ client: LOCAL for an Apache RocketMQ cluster, or CLOUD for a RocketMQ service of a cloud provider. With CLOUD the message traces go to a trace topic per region, in the format of the cloud service. | LOCAL | String | |
Whether the producer and the consumer record message traces, which the RocketMQ client sends to the trace topic RMQ_SYS_TRACE_TOPIC. The consumer of replyToTopic does not record traces. | false | boolean | |
The namespace of the topics and groups, which the RocketMQ client sends with each request. Set it for a RocketMQ instance that uses namespaces, such as a serverless instance. The consumer of replyToTopic does not use it. | String | ||
The address of the RocketMQ name server, as host:port. Separate several name servers with a semicolon. | localhost:9876 | String | |
The tag of each message that the producer sends. The CamelRocketMQOverrideTag header replaces it. If not set, the messages are sent with an empty tag. | String | ||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
The consumer group of the consumer. The consumers of a group share the messages of the topic, and each message goes to one of them. The consumer fails to start if it is not set. | String | ||
How the consumer selects the messages of the topic that it receives: tag uses the subscribeTags option, and sql uses the subscribeSql option. | tag | String | |
The SQL92 expression on the message properties that selects the messages that the consumer receives, when messageSelectorType is sql. The broker must have enablePropertyFilter set to true. The default 1 = 1 receives all messages. | 1 = 1 | String | |
The tags of the messages that the consumer receives, when messageSelectorType is tag. Separate several tags with ||, for example TagA||TagB. The default * receives all messages. | * | String | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The producer group of the producer. The producer fails to start if it is not set. | String | ||
The consumer group that receives the replies on replyToTopic, required with replyToTopic. Use a group per producer: in a shared group a reply can reach another producer, which ignores it, and the exchange times out. | String | ||
The topic on which the producer receives the reply of an InOut exchange. The reply must carry the keys of the request, one of which the producer generates to match the reply. If not set, an InOut exchange does not wait for a reply. | String | ||
Whether the producer waits for the send result before the exchange continues. When true, a send that fails, or whose status is not SEND_OK, fails the exchange. When false, an InOnly exchange continues as soon as the message is handed to the RocketMQ client, and a failed send is only logged. An InOut exchange always waits for the send result, and for the reply when replyToTopic is set. | true | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
How often, in milliseconds, the producer checks for InOut exchanges whose requestTimeoutMillis has passed. A timeout can be detected up to this interval late. | 1000 | long | |
How long, in milliseconds, the producer waits for the reply of an InOut exchange on replyToTopic before it fails the exchange with an ExchangeTimedOutException. A value of 0 or less waits for Integer.MAX_VALUE milliseconds (about 24 days). | 10000 | long | |
The access key of the RocketMQ ACL credentials. The producer and the consumer sign their requests only when both accessKey and secretKey are set. The consumer of replyToTopic does not use it. | String | ||
The secret key of the RocketMQ ACL credentials. The producer and the consumer sign their requests only when both accessKey and secretKey are set. The consumer of replyToTopic does not use it. | String |
Endpoint Options
The RocketMQ endpoint is configured using URI syntax:
rocketmq:topicName
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The access channel of the RocketMQ client: LOCAL for an Apache RocketMQ cluster, or CLOUD for a RocketMQ service of a cloud provider. With CLOUD the message traces go to a trace topic per region, in the format of the cloud service. Enum values:
| LOCAL | String | |
Whether the producer and the consumer record message traces, which the RocketMQ client sends to the trace topic RMQ_SYS_TRACE_TOPIC. The consumer of replyToTopic does not record traces. | false | boolean | |
The namespace of the topics and groups, which the RocketMQ client sends with each request. Set it for a RocketMQ instance that uses namespaces, such as a serverless instance. The consumer of replyToTopic does not use it. | String | ||
The address of the RocketMQ name server, as host:port. Separate several name servers with a semicolon. | localhost:9876 | String | |
The consumer group of the consumer. The consumers of a group share the messages of the topic, and each message goes to one of them. The consumer fails to start if it is not set. | String | ||
How the consumer selects the messages of the topic that it receives: tag uses the subscribeTags option, and sql uses the subscribeSql option. Enum values:
| tag | String | |
The SQL92 expression on the message properties that selects the messages that the consumer receives, when messageSelectorType is sql. The broker must have enablePropertyFilter set to true. The default 1 = 1 receives all messages. | 1 = 1 | String | |
The tags of the messages that the consumer receives, when messageSelectorType is tag. Separate several tags with ||, for example TagA||TagB. The default * receives all messages. | * | String | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
The producer group of the producer. The producer fails to start if it is not set. | String | ||
The consumer group that receives the replies on replyToTopic, required with replyToTopic. Use a group per producer: in a shared group a reply can reach another producer, which ignores it, and the exchange times out. | String | ||
The topic on which the producer receives the reply of an InOut exchange. The reply must carry the keys of the request, one of which the producer generates to match the reply. If not set, an InOut exchange does not wait for a reply. | String | ||
The tag of each message that the producer sends. The CamelRocketMQOverrideTag header replaces it. If not set, the messages are sent with an empty tag. | String | ||
Whether the producer waits for the send result before the exchange continues. When true, a send that fails, or whose status is not SEND_OK, fails the exchange. When false, an InOnly exchange continues as soon as the message is handed to the RocketMQ client, and a failed send is only logged. An InOut exchange always waits for the send result, and for the reply when replyToTopic is set. | true | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
How often, in milliseconds, the producer checks for InOut exchanges whose requestTimeoutMillis has passed. A timeout can be detected up to this interval late. | 1000 | long | |
How long, in milliseconds, the producer waits for the reply of an InOut exchange on replyToTopic before it fails the exchange with an ExchangeTimedOutException. A value of 0 or less waits for Integer.MAX_VALUE milliseconds (about 24 days). | 10000 | long | |
The access key of the RocketMQ ACL credentials. The producer and the consumer sign their requests only when both accessKey and secretKey are set. The consumer of replyToTopic does not use it. | String | ||
The secret key of the RocketMQ ACL credentials. The producer and the consumer sign their requests only when both accessKey and secretKey are set. The consumer of replyToTopic does not use it. | String |
Message Headers
The RocketMQ component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The topic of the message. The consumer sets it, and so does the producer for the reply of an InOut exchange. | String | |
| Constant: | The tag of the message, or null if it has none. | String | |
| Constant: | The keys of the message, separated by a space if there are several, or null if it has none. | String | |
CamelRocketMQOverrideTopicName (producer) Constant: | The topic to send the message to. It replaces the topic name of the endpoint. | String | |
CamelRocketMQOverrideTag (producer) Constant: | The tag of the message to send. It replaces the sendTag option. | String | |
CamelRocketMQOverrideMessageKey (producer) Constant: | The keys of the message to send, separated by a space if there are several. For an InOut exchange the producer adds a key that it generates to match the reply. | String | |
CamelRocketMQBrokerName (consumer) Constant: | The name of the broker that holds the message queue of the message. | String | |
CamelRocketMQQueueId (consumer) Constant: | The ID of the message queue of the topic that holds the message. | int | |
CamelRocketMQStoreSize (consumer) Constant: | The size in bytes of the message as the broker stored it. | int | |
CamelRocketMQQueueOffset (consumer) Constant: | The offset of the message in its message queue. | long | |
CamelRocketMQSysFlag (consumer) Constant: | The system flags of the message, a bit set that RocketMQ uses for compression and the transaction type. | int | |
CamelRocketMQBornTimestamp (consumer) Constant: | The time when the producer sent the message, in milliseconds since the epoch. | long | |
CamelRocketMQBornHost (consumer) Constant: | The address of the producer client that sent the message. | SocketAddress | |
CamelRocketMQStoreTimestamp (consumer) Constant: | The time when the broker stored the message, in milliseconds since the epoch. | long | |
CamelRocketMQStoreHost (consumer) Constant: | The address of the broker that stored the message. | SocketAddress | |
| Constant: | The ID of the message: the unique ID that the producer client created for it, or else the offset message ID that the broker built from its address and the commit log offset. | String | |
CamelRocketMQCommitLogOffset (consumer) Constant: | The physical offset of the message in the commit log of the broker. | long | |
CamelRocketMQBodyCrc (consumer) Constant: | The CRC32 checksum of the message body, which the broker computed when it stored the message. | int | |
CamelRocketMQReconsumeTimes (consumer) Constant: | How many times RocketMQ has delivered the message again after a failed consumption, 0 on the first delivery. | int | |
CamelRocketMQPreparedTransactionOffset (consumer) Constant: | For a message of a transaction, the commit log offset of its prepared (half) message. | long |
Message Body
The message body at each side of the RocketMQ component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Consumer |
| The body of the RocketMQ message. The consumer does not reply: the body the route ends with is not sent back. |
Producer input |
| What converts to a byte array: text, bytes, a stream or a file, sent as the body of the message. A Map, a POJO, a number or no body fails, and the broker rejects an empty body: marshal it first. |
Producer output |
| The body is left as it was. With the InOut pattern and a replyToTopic, the producer waits for the reply and the body is the reply as a byte array. Not known when |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
InOut Pattern
InOut Pattern based on Message Key. When the producer sends the message, a messageKey will be generated and append to the message’s key.
After the message sent, a consumer will listen to the topic configured by the parameter ReplyToTopic.
When a message from ReplyToTpic contains the key, it means that the reply received and continue routing.
If requestTimeoutMillis elapsed and no reply received, an exception will be thrown.
from("rocketmq:START_TOPIC?consumerGroup=c1")
.to(ExchangePattern.InOut, "rocketmq:INTERMEDIATE_TOPIC" +
"?producerGroup=intermediaProducer" +
"&consumerGroup=intermediateConsumer" +
"&replyToTopic=REPLY_TO_TOPIC" +
"&replyToConsumerGroup=replyToConsumerGroup" +
"&requestTimeoutMillis=30000")
.to("log:InOutRoute?showAll=true") Error handling
The consumer acknowledges a message to RocketMQ (CONSUME_SUCCESS) when the exchange completes. When the exchange fails, or is marked rollback only, the consumer answers RECONSUME_LATER, and RocketMQ delivers the message again with an increasing delay, up to the maximum reconsume times of the consumer group (16 by default); after that the message goes to the dead letter queue of the consumer group. To acknowledge a message whose processing failed, handle the exception in the route, for example with onException(…).handled(true).
Examples
Receive messages from a topic named from_topic, route to to_topic.
-
Java
-
XML
-
YAML
from("rocketmq:FROM_TOPIC?namesrvAddr=localhost:9876&consumerGroup=consumer")
.to("rocketmq:TO_TOPIC?namesrvAddr=localhost:9876&producerGroup=producer"); <route>
<from uri="rocketmq:FROM_TOPIC?namesrvAddr=localhost:9876&consumerGroup=consumer"/>
<to uri="rocketmq:TO_TOPIC?namesrvAddr=localhost:9876&producerGroup=producer"/>
</route> - route:
from:
uri: rocketmq:FROM_TOPIC
parameters:
namesrvAddr: "localhost:9876"
consumerGroup: consumer
steps:
- to:
uri: rocketmq:TO_TOPIC
parameters:
namesrvAddr: "localhost:9876"
producerGroup: producer Setting specific headers can change routing behaviour. For example, if header CamelRocketMQOverrideTopicName was set, the message will be sent to ACTUAL_TARGET instead of ORIGIN_TARGET.
-
Java
-
XML
-
YAML
from("rocketmq:FROM?consumerGroup=consumer")
.setHeader("CamelRocketMQOverrideTopicName", constant("ACTUAL_TARGET"))
.setHeader("CamelRocketMQOverrideTag", constant("OVERRIDE_TAG"))
.setHeader("CamelRocketMQOverrideMessageKey", constant("OVERRIDE_MESSAGE_KEY"))
.to("rocketmq:ORIGIN_TARGET?producerGroup=producer")
.to("log:RocketRoute?showAll=true"); <route>
<from uri="rocketmq:FROM?consumerGroup=consumer"/>
<setHeader name="CamelRocketMQOverrideTopicName">
<constant>ACTUAL_TARGET</constant>
</setHeader>
<setHeader name="CamelRocketMQOverrideTag">
<constant>OVERRIDE_TAG</constant>
</setHeader>
<setHeader name="CamelRocketMQOverrideMessageKey">
<constant>OVERRIDE_MESSAGE_KEY</constant>
</setHeader>
<to uri="rocketmq:ORIGIN_TARGET?producerGroup=producer"/>
<to uri="log:RocketRoute?showAll=true"/>
</route> - route:
from:
uri: rocketmq:FROM
parameters:
consumerGroup: consumer
steps:
- setHeader:
name: CamelRocketMQOverrideTopicName
constant: ACTUAL_TARGET
- setHeader:
name: CamelRocketMQOverrideTag
constant: OVERRIDE_TAG
- setHeader:
name: CamelRocketMQOverrideMessageKey
constant: OVERRIDE_MESSAGE_KEY
- to:
uri: rocketmq:ORIGIN_TARGET
parameters:
producerGroup: producer
- to:
uri: log:RocketRoute?showAll=true