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
The component level is the highest level which holds general and common configurations that are inherited by the endpoints. 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.
Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.
Configuring Endpoint Options
Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for 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, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.
The following two sections lists all the options, firstly for the component followed by the endpoint.
Component Options
The RocketMQ component supports 15 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Name server address of RocketMQ cluster. | localhost:9876 | String | |
Each message would be sent with this tag. | String | ||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. 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 | |
Consumer group name. | String | ||
Subscribe tags of consumer. Multiple tags could be split by , such as TagATagB. | * | 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 | |
Producer group name. | String | ||
Consumer group name used for receiving response. | String | ||
Topic used for receiving response when using in-out pattern. | String | ||
Whether waiting for send result before routing to next endpoint. | false | 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 | |
Check interval milliseconds of request timeout. | 1000 | long | |
Timeout milliseconds of receiving response when using in-out pattern. | 10000 | long | |
Access key for RocketMQ ACL. | String | ||
Secret key for RocketMQ ACL. | String |
Endpoint Options
The RocketMQ endpoint is configured using URI syntax:
rocketmq:topicName
with the following path and query parameters:
Query Parameters (16 parameters)
Name | Description | Default | Type |
---|---|---|---|
Name server address of RocketMQ cluster. | localhost:9876 | String | |
Consumer group name. | String | ||
Subscribe tags of consumer. Multiple tags could be split by , such as TagATagB. | * | String | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. 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 | ||
Producer group name. | String | ||
Consumer group name used for receiving response. | String | ||
Topic used for receiving response when using in-out pattern. | String | ||
Each message would be sent with this tag. | String | ||
Whether waiting for send result before routing to next endpoint. | false | 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 | |
Check interval milliseconds of request timeout. | 1000 | long | |
Timeout milliseconds of receiving response when using in-out pattern. | 10000 | long | |
Access key for RocketMQ ACL. | String | ||
Secret key for RocketMQ ACL. | String |
Message Headers
The RocketMQ component supports 20 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
Constant: | Topic of message. | String | |
Constant: | Tag of message. | String | |
Constant: | Key of message. | String | |
CamelRockerMQOverrideTopicName (producer) Constant: | If this header is set, the message will be routed to the topic specified by this header instead of the origin topic in endpoint. | String | |
CamelRockerMQOverrideTag (producer) Constant: | If this header is set, the message’s tag will be set to value specified by this header instead of the sendTag defined in endpoint. | String | |
CamelRockerMQOverrideMessageKey (producer) Constant: | Set keys for the message. When using in-out pattern, the value will be prepended to the generated keys. | String | |
CamelRockerMQBrokerName (consumer) Constant: | Broker name. | String | |
CamelRockerMQQueueId (consumer) Constant: | Queue ID. | int | |
CamelRockerMQStoreSize (consumer) Constant: | Store size. | int | |
CamelRockerMQQueueOffset (consumer) Constant: | Queue offset. | long | |
CamelRockerMQSysFlag (consumer) Constant: | Sys flag. | int | |
CamelRockerMQBornTimestamp (consumer) Constant: | Born timestamp. | long | |
CamelRockerMQBornHost (consumer) Constant: | Born host. | SocketAddress | |
CamelRockerMQStoreTimestamp (consumer) Constant: | Store timestamp. | long | |
CamelRockerMQStoreHost (consumer) Constant: | Store host. | SocketAddress | |
Constant: | Msg ID. | String | |
CamelRockerMQCommitLogOffset (consumer) Constant: | Commit log offset. | long | |
CamelRockerMQBodyCrc (consumer) Constant: | Body CRC. | int | |
CamelRockerMQReconsumeTimes (consumer) Constant: | Reconsume times. | int | |
CamelRockerMQPreparedTransactionOffset (consumer) Constant: | Prepard transaction offset. | long |
InOut Pattern
InOut Pattern based on Message Key. When the producer sending 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?producerGroup=p1&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")
Examples
Receive messages from a topic named from_topic
, route to to_topic
.
from("rocketmq:FROM_TOPIC?namesrvAddr=localhost:9876&consumerGroup=consumer")
.to("rocketmq:TO_TOPIC?namesrvAddr=localhost:9876&producerGroup=producer");
Setting specific headers can change routing behaviour. For example, if header RocketMQConstants.OVERRIDE_TOPIC_NAME
was set, the message will be sent to ACTUAL_TARGET
instead of ORIGIN_TARGET
.
from("rocketmq:FROM?consumerGroup=consumer")
.process(exchange -> {
exchange.getMessage().setHeader(RocketMQConstants.OVERRIDE_TOPIC_NAME, "ACTUAL_TARGET");
exchange.getMessage().setHeader(RocketMQConstants.OVERRIDE_TAG, "OVERRIDE_TAG");
exchange.getMessage().setHeader(RocketMQConstants.OVERRIDE_MESSAGE_KEY, "OVERRIDE_MESSAGE_KEY");
}
)
.to("rocketmq:ORIGIN_TARGET?producerGroup=producer")
.to("log:RocketRoute?showAll=true")