Camel Components

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, *.yaml files, 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

accessChannel (common)

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

enableTrace (common)

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

namespace (common)

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

namesrvAddr (common)

The address of the RocketMQ name server, as host:port. Separate several name servers with a semicolon.

localhost:9876

String

sendTag (common)

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

bridgeErrorHandler (consumer)

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

consumerGroup (consumer)

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

messageSelectorType (consumer)

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

subscribeSql (consumer)

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

subscribeTags (consumer)

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

lazyStartProducer (producer)

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

producerGroup (producer)

The producer group of the producer. The producer fails to start if it is not set.

String

replyToConsumerGroup (producer)

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

replyToTopic (producer)

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

waitForSendResult (producer)

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

autowiredEnabled (advanced)

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

requestTimeoutCheckerIntervalMillis (advanced)

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

requestTimeoutMillis (advanced)

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

accessKey (secret)

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

secretKey (secret)

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:

Path Parameters

Name Description Default Type

topicName (common)

Required The name of the topic. The consumer subscribes to this topic, and the producer sends to it unless the CamelRocketMQOverrideTopicName header names another topic.

String

Query Parameters

Name Description Default Type

accessChannel (common)

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

  • CLOUD

LOCAL

String

enableTrace (common)

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

namespace (common)

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

namesrvAddr (common)

The address of the RocketMQ name server, as host:port. Separate several name servers with a semicolon.

localhost:9876

String

consumerGroup (consumer)

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

messageSelectorType (consumer)

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

  • sql

tag

String

subscribeSql (consumer)

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

subscribeTags (consumer)

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

bridgeErrorHandler (consumer (advanced))

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

exceptionHandler (consumer (advanced))

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

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

producerGroup (producer)

The producer group of the producer. The producer fails to start if it is not set.

String

replyToConsumerGroup (producer)

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

replyToTopic (producer)

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

sendTag (producer)

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

waitForSendResult (producer)

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

lazyStartProducer (producer (advanced))

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

requestTimeoutCheckerIntervalMillis (advanced)

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

requestTimeoutMillis (advanced)

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

accessKey (security)

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

secretKey (security)

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

CamelRocketMQTopic (consumer)

Constant: TOPIC

The topic of the message. The consumer sets it, and so does the producer for the reply of an InOut exchange.

String

CamelRocketMQTag (consumer)

Constant: TAG

The tag of the message, or null if it has none.

String

CamelRocketMQKey (consumer)

Constant: KEY

The keys of the message, separated by a space if there are several, or null if it has none.

String

CamelRocketMQOverrideTopicName (producer)

Constant: OVERRIDE_TOPIC_NAME

The topic to send the message to. It replaces the topic name of the endpoint.

String

CamelRocketMQOverrideTag (producer)

Constant: OVERRIDE_TAG

The tag of the message to send. It replaces the sendTag option.

String

CamelRocketMQOverrideMessageKey (producer)

Constant: OVERRIDE_MESSAGE_KEY

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: BROKER_NAME

The name of the broker that holds the message queue of the message.

String

CamelRocketMQQueueId (consumer)

Constant: QUEUE_ID

The ID of the message queue of the topic that holds the message.

int

CamelRocketMQStoreSize (consumer)

Constant: STORE_SIZE

The size in bytes of the message as the broker stored it.

int

CamelRocketMQQueueOffset (consumer)

Constant: QUEUE_OFFSET

The offset of the message in its message queue.

long

CamelRocketMQSysFlag (consumer)

Constant: SYS_FLAG

The system flags of the message, a bit set that RocketMQ uses for compression and the transaction type.

int

CamelRocketMQBornTimestamp (consumer)

Constant: BORN_TIMESTAMP

The time when the producer sent the message, in milliseconds since the epoch.

long

CamelRocketMQBornHost (consumer)

Constant: BORN_HOST

The address of the producer client that sent the message.

SocketAddress

CamelRocketMQStoreTimestamp (consumer)

Constant: STORE_TIMESTAMP

The time when the broker stored the message, in milliseconds since the epoch.

long

CamelRocketMQStoreHost (consumer)

Constant: STORE_HOST

The address of the broker that stored the message.

SocketAddress

CamelRocketMQMsgId (consumer)

Constant: MSG_ID

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: COMMIT_LOG_OFFSET

The physical offset of the message in the commit log of the broker.

long

CamelRocketMQBodyCrc (consumer)

Constant: BODY_CRC

The CRC32 checksum of the message body, which the broker computed when it stored the message.

int

CamelRocketMQReconsumeTimes (consumer)

Constant: RECONSUME_TIMES

How many times RocketMQ has delivered the message again after a failed consumption, 0 on the first delivery.

int

CamelRocketMQPreparedTransactionOffset (consumer)

Constant: PREPARED_TRANSACTION_OFFSET

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

bytes (byte[])

The body of the RocketMQ message. The consumer does not reply: the body the route ends with is not sent back.

Producer input

text, bytes, stream or file

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

unchanged

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 replyToTopic is set.

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.

Java-only: uses ExchangePattern.InOut enum and string concatenation for the URI
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&amp;consumerGroup=consumer"/>
  <to uri="rocketmq:TO_TOPIC?namesrvAddr=localhost:9876&amp;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