Technical work, should be praised and then see, form a habitCopy the code

RocketMQ use tutorial related series of directories


Section one: Introduction

For example, in e-commerce, if you submit an order, you can send a delay message, check the status of the order one hour later, and cancel the order to release the inventory if the payment is still not made.

Its implementation is basically the same as that of ordinary message producers and consumers, with one more set delay level.

message.setDelayTimeLevel()
Copy the code

Currently, RocketMq does not support arbitrary latency, requiring several fixed latency levels ranging from 1s to 2h, corresponding to levels 1 to 18

Note: Aliyun charging version supports any time delay

private String messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h";
Copy the code

Section 2: Delayed message – Producer and message step description

Delay message producer code implementation steps

1. Create message producers, and specify producer group names

2. Specify the Nameserver address

3. Start the producer

4. Create a message object, specify the Topic, Tag, and message body, and set the delay level

5. Send the MESSAGE

6. Shut down producers

Delay message consumer code implementation steps

1. Create a Consumer group and name the Consumer group

2. Specify the Nameserver address

3. Subscribe to topics and tags

4. Set the callback function to process the message

5. Start consumers

Note: The consumer Topic and Tag need to be aligned with the producer

Section three: Delayed message producers

public class Producer { public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException { // 1. Create message producer producer DefaultMQProducer = new DefaultMQProducer(" demo_producer_DELay_group "); SetNamesrvAddr ("192.168.88.131:9876"); // 2. // 3. Start (); System.out.println(" producer start "); for (int i = 0; i < 10; I++) {// 4. Create a message object and specify the Topic, Tag, and body of the message. */ Message MSG = new Message("DelayTopic", "Tag1", ("Hello "+ I).getbytes ()); // set the delay time to 10s msg.setDelayTimeLevel(3); SendResult result = producer. Send (MSG); SendStatus status = result.getsendStatus (); System.out.println(" send result :" + result); Timeunit.seconds.sleep (1); } // 6. Shutdown (); }}Copy the code

Effect:

Section four: delayed message consumers

public class Consumer { public static void main(String[] args) throws Exception { // 1. DefaultMQPushConsumer Consumer = new DefaultMQPushConsumer("demo_producer_delay_group"); // 2. Specify the Nameserver address consumer.setNamesrvaddr ("192.168.88.131:9876"); // 3. Subscribe Topic and Tag consumer. Subscribe ("DelayTopic", "*"); // 4. Set the callback function, Handle the message consumer. RegisterMessageListener (new MessageListenerConcurrently () {/ / accept the message content @ Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { for (MessageExt msg : MSGS) {system.out.println (" MSG: [" + msg.getmsgid () + "], "+ new String(msg.getBody()) + ", " + (System.currentTimeMillis() - msg.getBornTimestamp())); } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; }}); Start consumer consumer.start(); System.out.println(" consumer start "); }}Copy the code

Effect:

The delay message is received 10 seconds later