【问题标题】:How to use BatchingRabbitTemplate to increase publishing speed如何使用 BatchingRabbitTemplate 提高发布速度
【发布时间】:2022-11-29 00:03:28
【问题描述】:

我的目标是向交易所发布 50000 条消息。我使用RabbitTemplate.convertAndSend循环发送每条消息。

由于此过程花费了大量时间,我开始寻找一种可以减少它的方法。我针对 BatchingRabbitTemplate 运行,按以下方式创建和配置它:

    BatchingStrategy strategy = new SimpleBatchingStrategy(1000, 1100, 3_000);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(30);
    scheduler.initialize();
    rabbitTemplate = new BatchingRabbitTemplate(strategy, scheduler);

并继续循环发送每条消息:

    for (int i = 0; i < 50000; i++) {
         ...
        rabbitTemplate.convertAndSend("my-exchange", "*.my-key.*", message);
    }

但是在那之后我没有注意到速度有任何提高。 当我用

        BatchingStrategy strategy = new SimpleBatchingStrategy(1000, Integer.MAX_VALUE, 30_000);

它甚至比简单的RabbitTemplate 还要糟糕。看来我没有配置好。如果有人解释为什么它没有导致预期的发布速度提高和/或提出解决方案,那就太好了?

【问题讨论】:

    标签: java rabbitmq spring-rabbit


    【解决方案1】:

    看起来你的BatchingRabbitTemplate 不是豆子。而且您没有为其提供 ConnectionFactory

    将调试断点放入BatchingRabbitTemplate.send() 并查看发送这些消息时发生了什么非常简单。

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多