Send and receive messages with ApsaraMQ for RocketMQ using the Apache RocketMQ Java SDK. This page covers both the gRPC protocol SDK (rocketmq-client-java) and the Remoting protocol SDK (rocketmq-client).
Prerequisites
Before you begin, make sure that you have:
An ApsaraMQ for RocketMQ instance with topics and group IDs created in the ApsaraMQ for RocketMQ console
The instance endpoint (for example,
rmq-cn-XXXX.rmq.aliyuncs.com:8080)The required SDK dependency added to your project. For version details, see Version guide
If you use a Serverless instance, check the SDK version requirements for public network access before proceeding.
gRPC protocol SDK
The rocketmq-client-java SDK communicates over the gRPC protocol. The following tables list sample code for each message type, hosted in the Apache RocketMQ clients repository.
For Spring Boot integration, see rocketmq-v5-client-spring-boot-samples.
When sending transactional messages with the gRPC protocol SDK, set a topic when you start the producer. Without a topic, transaction checks are delayed. If the message is not sent within four hours, the half-transactional message may be discarded.
Send messages
| Message type | Sample code |
|---|---|
| Normal message (synchronous) | ProducerNormalMessageExample.java |
| Normal message (asynchronous) | AsyncProducerExample.java |
| Ordered message | ProducerFifoMessageExample.java |
| Scheduled or delayed message | ProducerDelayMessageExample.java |
| Transactional message | ProducerTransactionMessageExample.java |
| Lightweight message | LiteProducerExample.java |
Consume messages
| Consumer type | Sample code |
|---|---|
| Push consumer | PushConsumerExample.java |
| Simple consumer (synchronous) | SimpleConsumerExample.java |
| Simple consumer (asynchronous) | AsyncSimpleConsumerExample.java |
| Lightweight push consumer | LitePushConsumerExample.java |
For more information about push consumers and simple consumers, see Consumer types.
Remoting protocol SDK
The rocketmq-client SDK communicates over the Remoting protocol. This section provides inline code for each message type.
For Spring Boot integration, see rocketmq-spring-boot-samples.
Common setup
All Remoting protocol examples share the same authentication and connection setup. Review this section first, then refer to the message-type-specific code below.
Authentication
How you initialize the producer or consumer depends on the access method:
Public endpoint -- Pass an
RPCHookwith the instance username and password: Get the instance username and password from the Intelligent Identity Recognition tab of the Resource Access Management console. Do not use your Alibaba Cloud account's AccessKey ID and AccessKey secret.private static RPCHook getAclRPCHook() { return new AclClientRPCHook(new SessionCredentials("<instance-username>", "<instance-password>")); } // Pass the RPCHook when creating the producer DefaultMQProducer producer = new DefaultMQProducer(getAclRPCHook());VPC endpoint -- No
RPCHookrequired. The server authenticates based on the VPC:DefaultMQProducer producer = new DefaultMQProducer();Serverless instance -- Pass an
RPCHookfor public network access. If password-free access over the internal network is enabled, noRPCHookis required.
Connection
// Group ID created in the ApsaraMQ for RocketMQ console
producer.setProducerGroup("<your-group-id>");
// Endpoint from the ApsaraMQ for RocketMQ console
// Use the domain name and port as shown. Do not add http:// or https://.
// Do not use a resolved IP address.
producer.setNamesrvAddr("<your-access-point>");Message traces (optional)
To enable cloud message tracing, set the access channel and enable traces:
producer.setAccessChannel(AccessChannel.CLOUD);
// Required for SDK v5.3.0 and later, in addition to setAccessChannel
producer.setEnableTrace(true);Placeholder reference
Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
<instance-username> | Instance username from the console | -- |
<instance-password> | Instance password from the console | -- |
<your-group-id> | Group ID created in the console | GID_example |
<your-access-point> | Endpoint from the console | rmq-cn-XXXX.rmq.aliyuncs.com:8080 |
<your-topic> | Topic created in the console | topic_example |
<your-order-topic> | Topic for ordered messages | order_topic_example |
<your-transaction-topic> | Topic for transactional messages | transaction_topic_example |
<your-transaction-group-id> | Dedicated group ID for transactional messages | GID_transaction_example |
<your-message-tag> | Message tag for filtering | TagA |
Normal messages
Normal messages suit most use cases where ordering and transactional guarantees are not required.
Ordered messages
Ordered messages guarantee FIFO delivery within the same sharding key. Use them for scenarios such as sequential event processing or real-time data synchronization.
Scheduled and delayed messages
Scheduled messages are delivered at a specific time. Delayed messages are delivered after a configurable delay. Both use the __STARTDELIVERTIME user property.
Consume scheduled and delayed messages
Consume scheduled and delayed messages the same way as normal messages. No additional configuration is required.
Transactional messages
Transactional messages use a two-phase commit protocol. The producer sends a half-transactional message, runs a local transaction, and then commits or rolls back. The broker periodically checks uncommitted transactions by calling checkLocalTransaction.
The group ID for transactional messages cannot be shared with other message types. Create a dedicated group ID for transaction producers.
Consume transactional messages
Consume transactional messages the same way as normal messages. No additional configuration is required.
Serverless instance public network access
To access a Serverless instance over the public network, your SDK must meet a minimum version requirement. Add the namespace configuration shown below.
Replace InstanceId with your actual instance ID.
Remoting protocol SDK (rocketmq-client >= 5.2.0)
Add the namespace to the producer or consumer:
// Producer
producer.setNamespaceV2("InstanceId");
// Consumer
consumer.setNamespaceV2("InstanceId");gRPC protocol SDK (rocketmq-client-java >= 5.0.6)
Set the namespace in the ClientConfiguration:
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
.setEndpoints(endpoints)
.setNamespace("InstanceId")
.setCredentialProvider(sessionCredentialsProvider)
.build();