This topic takes TCP-based Java as an example to help engineers who are new to Message Queue for Apache RocketMQ to build a Message Queue for Apache RocketMQ test project. The demo project contains test code for normal messages, ordered messages, transactional messages, and scheduled (delayed) messages, as well as Spring configurations.

Prerequisites

  • The integrated development environment (IDE) has been installed.

    You can use IntelliJ IDEA or Eclipse. This topic takes IntelliJ IDEA as an example.

    You can download IntelliJ IDEA Ultimate from https://www.jetbrains.com/idea/ and install it as instructed.

  • The demo project has been downloaded.

    You can download the demo project package from https://github.com/AliwareMQ/mq-demo to a local host and decompress it. Then, a folder named mq-demo-master is created on the local host.

  • The Java Development Kit (JDK) has been downloaded and installed.

Configure the demo project

  1. Import the demo project to IntelliJ IDEA
    1. On the IntelliJ IDEA page, click Import Project and then select the mq-demo-master folder.
    2. Select Maven.
    3. Click Next until the project is imported. The dependent JAR package needs to be loaded to the demo project, which takes two to three minutes.
  2. Create resources

    Create the required resources in the Message Queue for Apache RocketMQ console, including a Message Queue for Apache RocketMQ instance, a topic, a group ID, and an AccessKey pair for authentication.

    For more information and operation guides, see Step 2: Create resources.

  3. Configure the demo
    You need to configure the resource information created in step 2 to the MqConfig class and common.xml file.
    1. Configure the MqConfig class as follows:
      • public static final String TOPIC = "The topic that you created";
      • public static final String GROUP_ID = "The group ID that you created";
      • public static final String ACCESS_KEY = "The AccessKey ID of your Alibaba Cloud account;
      • public static final String SECRET_KEY = "The AccessKey secret of your Alibaba Cloud account";
      • public static final String NAMESRV_ADDR = "The TCP endpoint of your Message Queue for Apache RocketMQ instance. You can obtain TCP Endpoint from the Endpoint Information section on the Instance Details page.
      Note
      • For more information about how to create an AccessKey pair (including an AccessKey ID and an AccessKey secret), seeCreate an AccessKey
      • You can also use the AccessKey pair of the RAM user if it is granted with the permissions of the topic you created.
    2. Configure common.xml.
       <props>
       <prop key="AccessKey">XXX</prop> <! -- Modify the resource information before using it. -->
       <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 through main classes

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

      LocalTransactionCheckerImpl is an interface class that you can call to check local transactions. For more information, see Send and subscribe to transactional messages.

    • Send ordered messages: Run the SimpleOrderProducer class.

      In this mode, messages are delivered and consumed in order. For more information, see Send and subscribe to ordered messages.

    • Send scheduled (delayed) messages: Run the MQTimerProducer class. These messages are delivered with a delay of 3s.

      You can specify an exact delivery time. The delay of the delivery can be up to 40 days. For more information, see Send and subscribe to scheduled messages.

    Log on to the Message Queue for Apache RocketMQ console. In the left-side navigation pane, click Message Query. On the page that appears, click the By Topic tab. On the tab that appears, enter a topic name, and click the search icon. The query result indicates that the messages have been sent to the topic.
  2. Subscribe to messages
    • Subscribe to normal messages:
      • Subscribe to normal messages in Java mode: Run the SimpleMQConsumer class.
      • Subscribe to normal messages in Spring mode: Run the ConsumerClient class.
    • Subscribe to transactional messages: Run the SimpleMQConsumer class.
    • Subscribe to ordered messages: Run the SimpleOrderConsumer class.
    • Subscribe to scheduled (delayed) messages: Run the SimpleMQConsumer class.
    The log indicating that the messages have been received is printed. The class needs to be initialized, which takes several seconds to complete. Initialization seldom occurs in the production environment.
    Log on to the Message Queue for Apache RocketMQ console. In the left-side navigation pane, click Groups. Find the target group and click Consumer Status in the Actions column. The started consumers are online, and have subscribed to the same topics.

References