批量生产消息

1、简单的生产消息

public class SimpleBatchProducer {

public static void main(String[] args) throws Exception {

DefaultMQProducer producer = new DefaultMQProducer("BatchProducerGroupName");

producer.setNamesrvAddr("127.0.0.1:9876");

String topic = "BatchTest";

List messages = new ArrayList<>();

messages.add(new Message(topic, "Tag", "OrderID001", "Hello world 0".getBytes()));

messages.add(new Message(topic, "Tag", "OrderID002", "Hello world 1".getBytes())); 

messages.add(new Message(topic, "Tag", "OrderID003", "Hello world 2".getBytes())); 

producer.send(messages);

}

}

2、将消息拆分发送

public class SplitBatchProducer {

public static void main(String[] args) throws Exception {

    DefaultMQProducer producer = new     DefaultMQProducer("BatchProducerGroupName");

    producer.setNamesrvAddr("127.0.0.1:9876"); producer.start(); //large batch     String topic = "BatchTest2"; 

    List messages = new ArrayList<>(100 * 10000);

        for (int i = 0; i < 100 * 10000; i++) {

            messages.add(new Message(topic, "Tag", "OrderID" + i, ("Hello world " +             i).getBytes()));

    }//split the large batch into small ones:

     ListSplitter splitter = new ListSplitter(messages); 

     while (splitter.hasNext()) {

         List listItem = splitter.next();

            producer.send(listItem);

            Thread.sleep(4000);

        }

    }

}

大家可以定制自己的发送策略

class ListSplitter implements Iterator> { 

     private int sizeLimit = 1000 * 1000; private final List messages; private int currIndex;      public ListSplitter(List messages) {

        this.messages = messages;

        }

@Override public boolean hasNext() { return currIndex < messages.size(); }

 @Override public List next() {

        int nextIndex = currIndex;

        int totalSize = 0;

        for (; nextIndex < messages.size(); nextIndex++) {

        Message message = messages.get(nextIndex); int tmpSize =         message.getTopic().length() + message.getBody().length; Mapproperties =         message.getProperties(); for (Map.Entry entry : properties.entrySet()) {

                tmpSize += entry.getKey().length() + entry.getValue().length();

            }

            tmpSize = tmpSize + 20; //for log overhead

            if (tmpSize > sizeLimit) {

                //it is unexpected that single message exceeds the sizeLimit

                //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 > sizeLimit) {

                break;

            } else {

                totalSize += tmpSize;

            }

        } List subList = messages.subList(currIndex, nextIndex);

        currIndex = nextIndex;

        return subList;

        }

@Override

    public void remove() {

        throw new UnsupportedOperationException("Not allowed to remove");

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,281评论 19 139
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,205评论 0 10
  • 英文文档,一开始我也是抗拒的,边翻译边看,也就花费了1个小时基本就阅读过了,我的英文基础其实很差。附上链接:链接:...
    lonecolonel阅读 13,475评论 3 1
  • 到底为什么有的茶那么贵? 20克卖了19.8万元,几乎1克1万元。2014年,在上海举行的一场拍卖会上,被誉为“茶...
    沄间一杯茶阅读 2,206评论 0 0
  • 两千年眠沧海事,恨此杪头苦叹春。 无言不语倾悲怨,落笔难诉才亦空。
    痕之熠阅读 1,837评论 0 3

友情链接更多精彩内容