010 rocketmq批量消息

文章目录

批量消息

批量发送可以提⾼发送性能,但有⼀定的限制:

topic 相同

waitStoreMsgOK 相同 (⾸先我们建设消息的iswaitstoremsgok=true(默认为true), 如果没有异常,我们将始终收到"OK",org.apache.rocketmq.common.message.Message#isWaitStoreMsgOK)

不支持延时发送

⼀批消息的大小不能⼤于 4M(DefaultMQProducer.maxMessageSize)

大小限制需要特殊注意,因为消息是动态的,不注意的话就可能超限,就会报错:

计算消息的大小

= (topic + body + (key + value) * N) * 吞吐量

java 复制代码
int tmpSize = message.getTopic().length() + message.getBody().length;
Map<String, String> properties = message.getProperties();
for (Map.Entry<String, String> entry : properties.entrySet()) {
 tmpSize += entry.getKey().length() + entry.getValue().length();
}

BatchProducer.java

java 复制代码
package com.example.rocketmq.demo.batch;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BatchProducer {
    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
                DefaultMQProducer("please_rename_unique_group_name");
        // Specify name server addresses.
        producer.setNamesrvAddr("localhost:9876");
        //Launch the instance.
        producer.start();
        String topic = "TopicTest";
        List<Message> messages = new ArrayList<>();
        messages.add(new Message(topic, "TagA", "OrderID001", "Hello world 0".getBytes()));
        messages.add(new Message(topic, "TagA", "OrderID002", "Hello world 1".getBytes()));
        messages.add(new Message(topic, "TagA", "OrderID003", "Hello world 2".getBytes()));


        //then you could split the large list into small ones:
        ListSplitter splitter = new ListSplitter(messages);

        while (splitter.hasNext()) {
            try {
                List<Message>  listItem = splitter.next();
                SendResult sendResult = producer.send(listItem);
                System.out.printf("%s%n", sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                //handle the error
            }
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }
}

class ListSplitter implements Iterator<List<Message>> {
    private final int SIZE_LIMIT = 1024 * 1024 * 4;
    private final List<Message> messages;
    private int currIndex;
    public ListSplitter(List<Message> messages) {
        this.messages = messages;
    }
    @Override public boolean hasNext() {
        return currIndex < messages.size();
    }
    @Override public List<Message> next() {
        int nextIndex = currIndex;
        int totalSize = 0;
        for (; nextIndex < messages.size(); nextIndex++) {
                Message message = messages.get(nextIndex);


                //计算消息的大小 = (topic + body + (key + value) * N) * 吞吐量

                int tmpSize = message.getTopic().length() + message.getBody().length;
                //属性值的添加
                Map<String, String> properties = message.getProperties();
                for (Map.Entry<String, String> entry : properties.entrySet()) {
                    //+key + value
                    tmpSize += entry.getKey().length() + entry.getValue().length();
                }




                tmpSize = tmpSize + 20; //for log overhead
                if (tmpSize > SIZE_LIMIT) {
                    //it is unexpected that single message exceeds the SIZE_LIMIT
                    //here just let it go, otherwise it will block the splitting process
                    if (nextIndex - currIndex == 0) {
                        //if the next sublist has no element, add this one and then break, otherwise just break
                        nextIndex++;
                    }
                    break;
                }
                if (tmpSize + totalSize > SIZE_LIMIT) {
                    break;
                } else {
                    totalSize += tmpSize;
                }

        }
        List<Message> subList = messages.subList(currIndex, nextIndex);
        currIndex = nextIndex;
        return subList;
    }
}

BatchConsumer.java

java 复制代码
package com.example.rocketmq.demo.batch;

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.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class BatchConsumer {
    public static void main(String[] args) throws InterruptedException, MQClientException {

        // Instantiate with specified consumer group name.
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");

        // Specify name server addresses.
        consumer.setNamesrvAddr("localhost:9876");

        // Subscribe one more more topics to consume.
        consumer.subscribe("TopicTest", "*");
        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                                                            ConsumeConcurrentlyContext context) {
                System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);


                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        //Launch the consumer instance.
        consumer.start();

        System.out.printf("Consumer Started.%n");
    }
}
相关推荐
King Gigi.1 天前
RocketMQ的事务消息机制
数据库·rocketmq·java-rocketmq
蒋大钊!2 天前
RocketMQ与Kafka的区别
kafka·rocketmq
小健学 Java3 天前
Kafka 与 RabbitMQ、RocketMQ 有何不同?
kafka·rabbitmq·rocketmq
搞不懂语言的程序员4 天前
Kafka与RocketMQ在事务消息实现上的区别是什么?
分布式·kafka·rocketmq
小园子的小菜5 天前
探秘 RocketMQ 的 DLedgerServer:MemberState 的技术解析与深度剖析
java·rocketmq·java-rocketmq
记得开心一点嘛5 天前
Rockermq的部署与使用(0-1)
java·rocketmq
计算机毕设定制辅导-无忧学长6 天前
ActiveMQ 与其他 MQ 的对比分析:Kafka/RocketMQ 的选型参考(一)
kafka·rocketmq·activemq
YUELEI1186 天前
Centos9 安装 RocketMQ5
centos·rocketmq
-273K7 天前
39.RocketMQ高性能核心原理与源码架构剖析
架构·rocketmq
方二华10 天前
分布式队列对消息语义的处理
分布式·kafka·rocketmq