系列十一、Java操作RocketMQ之批量消息(分片批量消息)

一、案例代码

1.1、pom

        同系列五

1.2、RocketMQConstant

        同系列五

1.3、BatchConsumer

        同系列十

1.4、BatchSplitProducer

package org.star.batch.producer;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.star.batch.splitter.MessageSplitter;
import org.star.constants.RocketMQConstant;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 一叶浮萍归大海
 * @Date: 2023/8/30 09:45
 * @Description: 分片批量消息生产者
 */
@Slf4j
public class BatchSplitProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("BatchSplitProducerGroup");
        producer.setNamesrvAddr(RocketMQConstant.NAME_SERVER_ADDR);
        producer.start();
        log.info("分片批量消息生产者 start success");
        List messages = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            messages.add(new Message("BatchTopic", ("我是分片批量消息,消息编号[" + i + "]").getBytes(StandardCharsets.UTF_8)));
        }
        MessageSplitter splitter = new MessageSplitter(messages);
        while (splitter.hasNext()) {
            List messageList = splitter.next();
            SendResult result = producer.send(messageList);
            log.info("BatchSplitProducer sendStatus:{},queueId:{},msgId:{}", result.getSendStatus(), result.getMessageQueue().getQueueId(), result.getMsgId());
        }
        producer.shutdown();
    }
}

1.5、控制台打印

# 生产者端
22:55:52.544 [main] INFO org.star.batch.producer.BatchSplitProducer - 分片批量消息生产者 start success
22:55:53.884 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:2,msgId:C0A81FB28EB018B4AAC20F38640F0000
22:55:53.888 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:3,msgId:C0A81FB28EB018B4AAC20F38641E0001
22:55:53.895 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:0,msgId:C0A81FB28EB018B4AAC20F3864200002
22:55:53.898 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:1,msgId:C0A81FB28EB018B4AAC20F3864270003
22:55:53.901 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:2,msgId:C0A81FB28EB018B4AAC20F38642A0004
22:55:53.904 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:3,msgId:C0A81FB28EB018B4AAC20F38642D0005
22:55:53.909 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:0,msgId:C0A81FB28EB018B4AAC20F3864300006
22:55:53.912 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:1,msgId:C0A81FB28EB018B4AAC20F3864360007
22:55:53.916 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:2,msgId:C0A81FB28EB018B4AAC20F3864390008
22:55:53.919 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:3,msgId:C0A81FB28EB018B4AAC20F38643C0009
22:55:53.924 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:0,msgId:C0A81FB28EB018B4AAC20F38643F000A
22:55:53.927 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:1,msgId:C0A81FB28EB018B4AAC20F386445000B
22:55:53.931 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:2,msgId:C0A81FB28EB018B4AAC20F386447000C
22:55:53.934 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:3,msgId:C0A81FB28EB018B4AAC20F38644B000D
22:55:53.937 [main] INFO org.star.batch.producer.BatchSplitProducer - BatchSplitProducer sendStatus:SEND_OK,queueId:0,msgId:C0A81FB28EB018B4AAC20F38644E000E


# 消费者端
22:55:43.072 [main] INFO org.star.batch.consumer.BatchConsumer - BatchConsumer start success!
22:55:53.919 [ConsumeMessageThread_2] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_2 收到消息 body:我是分片批量消息,消息编号[0]
22:55:53.918 [ConsumeMessageThread_3] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_3 收到消息 body:我是分片批量消息,消息编号[2]
22:55:53.918 [ConsumeMessageThread_5] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_5 收到消息 body:我是分片批量消息,消息编号[4]
22:55:53.918 [ConsumeMessageThread_7] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_7 收到消息 body:我是分片批量消息,消息编号[6]
22:55:53.921 [ConsumeMessageThread_10] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_10 收到消息 body:我是分片批量消息,消息编号[9]
22:55:53.919 [ConsumeMessageThread_9] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_9 收到消息 body:我是分片批量消息,消息编号[8]
22:55:53.918 [ConsumeMessageThread_8] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_8 收到消息 body:我是分片批量消息,消息编号[7]
22:55:53.918 [ConsumeMessageThread_1] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_1 收到消息 body:我是分片批量消息,消息编号[1]
22:55:53.919 [ConsumeMessageThread_6] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_6 收到消息 body:我是分片批量消息,消息编号[5]
22:55:53.918 [ConsumeMessageThread_4] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_4 收到消息 body:我是分片批量消息,消息编号[3]
22:55:53.925 [ConsumeMessageThread_11] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_11 收到消息 body:我是分片批量消息,消息编号[10]
22:55:53.929 [ConsumeMessageThread_12] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_12 收到消息 body:我是分片批量消息,消息编号[11]
22:55:53.933 [ConsumeMessageThread_13] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_13 收到消息 body:我是分片批量消息,消息编号[12]
22:55:53.937 [ConsumeMessageThread_14] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_14 收到消息 body:我是分片批量消息,消息编号[13]
22:55:53.940 [ConsumeMessageThread_15] INFO org.star.batch.consumer.BatchConsumer - ConsumeMessageThread_15 收到消息 body:我是分片批量消息,消息编号[14]

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