系列六、Java操作RocketMQ简单消息之异步消息

一、概述

        异步消息通常应用在对响应时间比较敏感的业务场景中,即发送端不能长时间的容忍等待Broker的响应,发送端发送完消息后会有一个异步消息通知。

二、案例代码

2.1、pom

        同系列五

2.2、RocketMQConstant

        同系列五

2.3、SimpleConsumer

        同系列五

2.4、AsyncProducer

package org.star.simple.producer;

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

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 一叶浮萍归大海
 * @Date: 2023/8/25 11:38
 * @Description: 异步发送生产者
 */
@Slf4j
public class AsyncProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("AsyncProducerGroup");
        producer.setNamesrvAddr(RocketMQConstant.NAME_SERVER_ADDR);
        producer.start();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            Message message = new Message("SimpleTopic", ("我是第[" + i + "]个异步消息").getBytes(StandardCharsets.UTF_8));
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("第[" + index + "]个简单消息发送成功 sendStatus:{},msgId:{},topic:{}", sendResult.getSendStatus(), sendResult.getMsgId(), sendResult.getMessageQueue().getTopic());
                }

                @Override
                public void onException(Throwable throwable) {
                    log.info("第[" + index + "]个消息发送失败,reason:{}", throwable.getMessage());
                }
            });
        }
        log.info("我先执行...");
    }

}

2.5、控制台打印

# 生产者端
22:56:07.067 [main] INFO org.star.simple.producer.AsyncProducer - 我先执行...
22:56:08.860 [NettyClientPublicExecutor_5] INFO org.star.simple.producer.AsyncProducer - 第[6]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860004,topic:SimpleTopic
22:56:08.868 [NettyClientPublicExecutor_8] INFO org.star.simple.producer.AsyncProducer - 第[0]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860000,topic:SimpleTopic
22:56:08.860 [NettyClientPublicExecutor_1] INFO org.star.simple.producer.AsyncProducer - 第[4]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860001,topic:SimpleTopic
22:56:08.870 [NettyClientPublicExecutor_5] INFO org.star.simple.producer.AsyncProducer - 第[9]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242900009,topic:SimpleTopic
22:56:08.860 [NettyClientPublicExecutor_7] INFO org.star.simple.producer.AsyncProducer - 第[5]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860002,topic:SimpleTopic
22:56:08.860 [NettyClientPublicExecutor_4] INFO org.star.simple.producer.AsyncProducer - 第[2]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860006,topic:SimpleTopic
22:56:08.870 [NettyClientPublicExecutor_8] INFO org.star.simple.producer.AsyncProducer - 第[8]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242900008,topic:SimpleTopic
22:56:08.860 [NettyClientPublicExecutor_3] INFO org.star.simple.producer.AsyncProducer - 第[3]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860005,topic:SimpleTopic
22:56:08.860 [NettyClientPublicExecutor_2] INFO org.star.simple.producer.AsyncProducer - 第[1]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860007,topic:SimpleTopic
22:56:08.860 [NettyClientPublicExecutor_6] INFO org.star.simple.producer.AsyncProducer - 第[7]个简单消息发送成功 sendStatus:SEND_OK,msgId:C0A81FB22A0018B4AAC20A1242860003,topic:SimpleTopic


# 消费者端
22:56:08.855 [ConsumeMessageThread_7] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[4]个异步消息
22:56:08.855 [ConsumeMessageThread_1] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[3]个异步消息
22:56:08.856 [ConsumeMessageThread_3] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[7]个异步消息
22:56:08.858 [ConsumeMessageThread_5] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[6]个异步消息
22:56:08.858 [ConsumeMessageThread_10] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[1]个异步消息
22:56:08.858 [ConsumeMessageThread_9] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[2]个异步消息
22:56:08.860 [ConsumeMessageThread_4] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[0]个异步消息
22:56:08.860 [ConsumeMessageThread_2] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[9]个异步消息
22:56:08.860 [ConsumeMessageThread_8] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[5]个异步消息
22:56:08.860 [ConsumeMessageThread_6] INFO org.star.simple.consumer.SimpleConsumer - 收到消息 body:我是第[8]个异步消息

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