edit-icon download-icon

Set up the project

Last Updated: Oct 16, 2018

Demo project setting includes the following steps:

Import MQ Demo Project to IDEA

Prerequisite: If JDK is not installed on your local disk, please download and install it first.

  1. In IDEA, select Import Project, and select folder mq-demo-master.

    IDEA Import Demo

  2. Select Import Type.

    idea-import-2

  3. Click Next until the import is completed. Dependent JAR package needs to be downloaded for the demo project, so the import process will take 2-3 minutes.

Create MQ Resources

Create Topic, Producer ID (PID), and Consumer ID (CID) in the MQ console.

  1. Log on to the MQ console.

  2. Select Internet region (default), click Create Topic on the right side, enter topic name (as unique as possible to prevent repetition), select message type, enter description, and click OK.

  3. Click Create Producer in the Actions column to create a producer ID.

  4. Click Create Consumer in the Actions column to create a consumer ID.

For detailed information, see Step 2: Create resources in Quick start guide.

Configure MQ Demo

3 files need to be configured: MqConfig class, producer.xml, and consumer.xml.

  1. Configure MqConfig class as follows.

    • public static final String TOPIC = “the topic just created”;
    • public static final String PRODUCER_ID = “the PID just created”;
    • public static final String CONSUMER_ID = “the CID just created”;
    • public static final String ACCESS_KEY = “AK of your Alibaba Cloud Account”;
    • public static final String SECRET_KEY = “SK of your Alibaba Cloud Account”;

    Log on your Alibaba Cloud Account, and click AccessKeys to obtain accesskeys. AK refers to Access Key ID and SK Access Key Secret.

    Note: You can also use the AK and SK of the RAM sub-account if it is authorized in the MQ console when the topic is created for the primary account.

  2. Configure producer.xml.

  1. <bean id="producer" class="com.aliyun.openservices.ons.api.bean.ProducerBean"
  2. init-method="start" destory-method="shutdown">
  3. <property name="properties">
  4. <map>
  5. <entry key="ProducerId" value="XXX"/><!——Replace CID——>
  6. <entry key="AccessKey" value="XXX"/><!——Replace ACCESS_KEY——>
  7. <entry key="SecretKey" value="XXX"/><!——Replace SECRET KEY——>
  8. <entry key="ONSAddr" value="http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet"/>
  9. </map>
  10. </property>
  11. </bean>
  1. Configure consumer.xml.
  1. <bean id="consumer" class="com.aliyun.openservices.ons.api.bean.Consumerbean"
  2. init-method="start" destory-method="shutdown">
  3. <property name="properties">
  4. <map>
  5. <entry key="ConsumerId" value="XXX"/><!——Replace CID——>
  6. <entry key="AccessKey" value="XXX"/><!——Replace ACCESS_KEY——>
  7. <entry key="SecretKey" value="XXX"/><!——Replace SECRET KEY——>
  8. <entry key="ONSAddr" value="http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet"/>
  9. </map>
  10. </property>
  11. <property name="subscriptionTable">
  12. <map>
  13. <entry value-ref="messageListener">
  14. <key>
  15. <bean class="com.aliyun.openservices.ons.api.bean.Subscription">
  16. <property name="topic" value="XXX"/><!——Replace Topic——>
  17. <property name="expression" value="*"/><!——MessageType: use "||" to split multiple messageTypes——>
  18. </bean>
  19. </key>
  20. </entry>
  21. </map>
  22. </property>
  23. </bean>
Thank you! We've received your feedback.