系列八、Java操作RocketMQ之异步消息

一、概述

        延迟消息放入RocketMQ后,过一段时间才会被监听到,然后消费。通常适用于消息发送后不想让消费者立即消费,例如下订单业务,订单提交发送一个延时消息,30min后去检查这个订单的状态,如果订单的状态还是未付款,就取消订单释放库存。

二、案例代码

2.1、pom

        同案例五

2.2、RocketMQConstant

        同案例五

2.3、DelayConsumer

package org.star.delay.consumer;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.star.constants.RocketMQConstant;

import java.util.List;

/**
 * @Author: 一叶浮萍归大海
 * @Date: 2023/8/29 17:10
 * @Description:
 */
@Slf4j
public class DelayConsumer {

    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("DelayConsumerGroup");
        consumer.setNamesrvAddr(RocketMQConstant.NAME_SERVER_ADDR);
        consumer.subscribe("DelayTopic","*");
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                if (CollectionUtils.isNotEmpty(list)) {
                    String body = StrUtil.utf8Str(list.get(0).getBody());
                    log.info("收到消息 body:{}",body);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        log.info("DelayConsumer start success!");
    }

}

2.4、DelayProducer

package org.star.delay.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.star.constants.RocketMQConstant;

import java.nio.charset.StandardCharsets;

/**
 * @Author: 一叶浮萍归大海
 * @Date: 2023/8/29 16:38
 * @Description:
 *  messageDelayLevel对应的等待时间
 *  1  ==》1s
 *  2  ==》5s
 *  3  ==》10s
 *  4  ==》30s
 *  5  ==》1m
 *  6  ==》2m
 *  7  ==》3m
 *  8  ==》4m
 *  9  ==》5m
 *  10 ==》6m
 *  11 ==》7m
 *  12 ==》8m
 *  13 ==》9m
 *  14 ==》10m
 *  15 ==》20m
 *  16 ==》30m
 *  17 ==》1h
 *  18 ==》2h
 */
@Slf4j
public class DelayProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("DelayProducerGroup");
        producer.setNamesrvAddr(RocketMQConstant.NAME_SERVER_ADDR);
        producer.start();

        Message message = new Message("DelayTopic", ("我是延迟消息").getBytes(StandardCharsets.UTF_8));
        // 延迟10s发送
        message.setDelayTimeLevel(3);
        producer.send(message);

        log.info("延迟消息发送成功");

        producer.shutdown();
    }

}

2.5、控制台打印

# 生产者端
19:28:21.347 [main] INFO org.star.delay.producer.DelayProducer - 延迟消息发送成功

# 消费者端
19:28:31.350 [ConsumeMessageThread_3] INFO org.star.delay.consumer.DelayConsumer - 收到消息 body:我是延迟消息

你可能感兴趣的:(RocketMQ系列,java-rocketmq,java,rocketmq)