Build and run a test project with the TCP client SDK for Java. The demo project includes working examples for five message types -- normal, ordered, transactional, scheduled, and delayed -- across Java, Spring, and Spring Boot.
Prerequisites
Before you begin, make sure that you have:
IntelliJ IDEA Ultimate Edition or Eclipse installed (the examples below use IntelliJ IDEA)
JDK installed
The demo project downloaded and decompressed on your local machine (the extracted folder is named
rocketmq-demo-master)An ApsaraMQ for RocketMQ instance with the following resources created in the ApsaraMQ for RocketMQ console: instance, topic, consumer group, AccessKey ID, and AccessKey secret. For instructions, see Create resources
Configure the demo project
Step 1: Import the project
Import the demo project files into IntelliJ IDEA.
Step 2: Update the SDK version
In pom.xml, update the TCP client SDK for Java dependency to the latest version. For available versions, see Release notes.
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>ons-client</artifactId>
<!--Replace with the latest version from the release notes-->
<version>1.8.8.5.Final</version>
</dependency>Step 3: Configure the MqConfig class
Set the following fields in the MqConfig class to match the resources you created:
public static final String TOPIC = "<your-topic>";
public static final String GROUP_ID = "<your-group-id>";
public static final String ORDER_TOPIC = "<your-order-topic>";
public static final String ORDER_GROUP_ID = "<your-order-group-id>";
public static final String ACCESS_KEY = "<your-access-key>";
public static final String SECRET_KEY = "<your-secret-key>";
public static final String TAG = "<your-message-tag>";
public static final String NAMESRV_ADDR = "<your-tcp-endpoint>";Replace the placeholders with your actual values:
| Placeholder | Description | Where to find it |
|---|---|---|
<your-topic> | Topic for normal and transactional messages | ApsaraMQ for RocketMQ console > your instance > Topics |
<your-group-id> | Consumer group ID | ApsaraMQ for RocketMQ console > your instance > Groups |
<your-order-topic> | Topic for ordered messages | Same as above (create a separate topic for ordered messages) |
<your-order-group-id> | Consumer group for ordered messages | Same as above |
<your-access-key> | AccessKey ID of your Alibaba Cloud account | See Create an AccessKey pair |
<your-secret-key> | AccessKey secret of your Alibaba Cloud account | Same as above |
<your-message-tag> | Custom message tag for filtering | Any string value |
<your-tcp-endpoint> | TCP endpoint of your instance | ApsaraMQ for RocketMQ console > your instance > Instance Details page > TCP Endpoint section |
You can also use the AccessKey pair of a RAM user who has permissions on the topic. For details about available configuration parameters, see Methods and parameters.
Step 4: Configure common.xml (Spring and Spring Boot only)
If you plan to use the Spring or Spring Boot examples, update the common.xml file with your resource information:
<props>
<prop key="AccessKey">XXX</prop> <!--Replace with your AccessKey ID-->
<prop key="SecretKey">XXX</prop> <!--Replace with your AccessKey secret-->
<prop key="GROUP_ID">XXX</prop> <!--Replace with your consumer group ID-->
<prop key="Topic">XXX</prop> <!--Replace with your topic-->
<prop key="NAMESRV_ADDR">XXX</prop> <!--Replace with your TCP endpoint-->
</props>Send and receive messages
The demo project runs producers and consumers in a single thread. For production systems, run producers and consumers on separate threads or hosts. For multi-threaded examples, see Send messages by using multiple threads.
Run each example by calling the main method of the corresponding class.
Send messages
Run the producer class that matches your message type and framework:
| Message type | Java | Spring | Spring Boot |
|---|---|---|---|
| Normal | SimpleMQProducer | ProducerClient | ProducerClient |
| Transactional | SimpleTransactionProducer | TransactionProducerClient | TransactionProducerClient |
| Ordered (FIFO) | SimpleOrderProducer | OrderProducerClient | OrderProducerClient |
| Scheduled and delayed | MQTimerProducer | Not supported | Not supported |
Details by message type:
Transactional messages: The
LocalTransactionCheckerImplclass handles local transaction status checks. For the full workflow, see Send and receive transactional messages.Ordered messages: Messages are sent and consumed in first-in-first-out (FIFO) order. For details, see Send and subscribe to ordered messages.
Scheduled and delayed messages: The
MQTimerProducerclass delivers messages after a 3-second delay by default. The maximum configurable delay is 40 days. For details, see Send and receive scheduled messages.
Verify that messages are sent
After sending messages, go to the ApsaraMQ for RocketMQ console and query messages by topic. The messages you sent should appear in the results.
Receive messages
Run the consumer class that matches your message type and framework:
| Message type | Java | Spring | Spring Boot |
|---|---|---|---|
| Normal | SimpleMQConsumer | ConsumerClient | ConsumerClient |
| Transactional | SimpleMQConsumer | ConsumerClient | ConsumerClient |
| Ordered (FIFO) | SimpleOrderConsumer | OrderConsumerClient | OrderConsumerClient |
| Scheduled and delayed | SimpleMQConsumer | Not supported | Not supported |
Class initialization takes a few seconds on startup. This is expected and only happens once -- in production, classes are initialized only during application startup.
Verify that messages are received
A log entry confirms that the consumer received the message. To verify further, view the status of consumers in the ApsaraMQ for RocketMQ console. The consumers you started should appear as online and subscribed to the topic.