This topic describes how to build a ApsaraMQ for RocketMQ test project by using the TCP client SDK for Java. The demo project provides sample code for normal messages, ordered messages, transactional messages, scheduled messages, and delayed messages. The demo project also provides Spring configurations for testing.

Prerequisites

  • The integrated development environment (IDE) is installed.

    You can use IntelliJ IDEA or Eclipse. IntelliJ IDEA is used in the examples in this topic.

    Make sure that IntelliJ IDEA Ultimate Edition is downloaded and installed. For more information, visit the download page.

  • The demo project package is downloaded.

    Download the demo project package and decompress the package to your local machine. A folder named rocketmq-demo-master is created. The folder contains the sample code for Java, Spring, and Spring Boot. For more information, see rocketmq-demo.

  • The Java Development Kit (JDK) is installed. For more information, see Java Downloads.

Configure the demo project

  1. Import the demo project files to IntelliJ IDEA.
  2. Create resources.

    Go to the Message Queue for Apache RocketMQ console to create resources, including the instance, topic, consumer group, AccessKey ID (AK), and AccessKey secret (SK).

    For more information, see Create resources.

  3. Configure the demo.
    Configure the MqConfig class and common.xml file based on the information about resources that are created in step 2.
    1. Modify the pom.xml file. We recommend that you change the version of the TCP client SDK for Java to the latest version. For information about SDK versions, see Release notes.
      <dependency>
          <groupId>com.aliyun.openservices</groupId>
          <artifactId>ons-client</artifactId>
          <!--We recommend that you replace the following version number with the latest version number of the TCP client SDK for Java-->
          <version>1.8.8.5.Final</version>
      </dependency>                            
    2. Configure the MqConfig class by referring to the following sample code:
      public static final String TOPIC = "The topic that you created";
      public static final String GROUP_ID = "The consumer group that you created";
      public static final String ORDER_TOPIC = "The topic that you created to send and receive ordered messages";
      public static final String GROUP_ID = "The consumer group that you created to send and receive ordered messages";
      public static final String ACCESS_KEY = "The AccessKey ID of your Alibaba Cloud account". For information about how to obtain an AccessKey ID, see Create an AccessKey pair";
      public static final String SECRET_KEY = "The AccessKey secret of your Alibaba Cloud account. For information about how to obtain an AccessKey secret, see Create an AccessKey pair";
      public static final String TAG = "The custom tag of the message";
      public static final String NAMESRV_ADDR = "The TCP endpoint that can be used to access your ApsaraMQ for RocketMQ instance. You can view the endpoint in the TCP Endpoint section of the Instance Details page in the ApsaraMQ for RocketMQ console;                              
      Note
      • You can also use the AccessKey pair of the RAM user who is granted the permissions on the topic that you created.
      • For more information, see Methods and parameters.
    3. Configure the common.xml file.
      <props>
          <prop key="AccessKey">XXX</prop> <!-- Modify the values based on your resources. -->
          <prop key="SecretKey">XXX</prop>
          <prop key="GROUP_ID">XXX</prop>
          <prop key="Topic">XXX</prop>
          <prop key="NAMESRV_ADDR">XXX</prop>
      </props>

Run the demo by calling the main method

  1. Send messages.
    • Send normal messages:
      • Java: Run the SimpleMQProducer class.
      • Spring: Run the ProducerClient class.
      • Spring Boot: Run the ProducerClient class.
    • Send transactional messages:
      • Java: Run the SimpleTransactionProducer class.

        The LocalTransactionCheckerImpl class is used to check the status of local transactions. For more information, see Send and receive transactional messages.

      • Spring: Run the TransactionProducerClient class.
      • Spring Boot: Run the TransactionProducerClient class.
    • Send ordered messages:
      • Java: Run the SimpleOrderProducer class.
      • Spring: Run the OrderProducerClient class.
      • Spring Boot: Run the OrderProducerClient class.

      Ordered messages are sent and consumed in first-in-first-out (FIFO) order. For more information, see Send and subscribe to ordered messages.

    • Send scheduled messages and delayed messages: Run the MQTimerProducer class. Messages are delivered after a delay of 3 seconds.

      You can also specify a delay time to deliver these messages. The maximum value of the delay time is 40 days. For more information, see Send and receive scheduled messages.

    In the ApsaraMQ for RocketMQ console, query messages by topic. You can view that the messages are sent to the topic.
  2. Receive messages.
    • Receive normal messages:
      • Java: Run the SimpleMQConsumer class.
      • Spring: Run the ConsumerClient class.
      • Spring Boot: Run the ConsumerClient class.
    • Receive transactional messages:
      • Java: Run the SimpleMQConsumer class.
      • Spring: Run the ConsumerClient class.
      • Spring Boot: Run the ConsumerClient class.
    • Receive ordered messages:
      • Java: Run the SimpleOrderConsumer class.
      • Spring: Run the OrderConsumerClient class.
      • Spring Boot: Run the OrderConsumerClient class.
    • Receive scheduled messages and delayed messages: Run the SimpleMQConsumer class.
      Note Spring and Spring Boot do not support the transmission of scheduled and delayed messages.
    A log is printed. The log indicates that the message is received. Class initialization requires several seconds to complete. In production environments, class initialization is seldom performed.

Verify the result: View the status of consumers in the ApsaraMQ for RocketMQ console. You can find that the started consumers are online and have subscribed to the same topic.

References