spring-amqp - 有什么办法可以限制制作者创建的 channel 吗?

标签 spring-amqp spring-rabbit

我们有一个应用程序可以非常快地生成消息并且需要 ACK。

这是一个愚蠢的模拟:

    @Bean
    public CachingConnectionFactory ccf() {
        var ccf = new CachingConnectionFactory("localhost");
        ccf.setPublisherConfirmType(CORRELATED);
        ccf.setPublisherReturns(true);
        return ccf;
    }


    @Bean
    public ApplicationRunner run(RabbitTemplate template) {
        return args -> {
            for (int i = 1; i < 10001; i++) {
                template.convertAndSend("poc", "hey");
                if (i % 1000 == 0) {
                    LOG.info("{}", i);
            }   }
        };
    }

这段代码最终创建和销毁了多个 channel ,有什么方法可以限制同时创建的 channel 吗? (阻塞或排队)

这是在 Rabbit 控制台中保留缓存的 channel 之一:127.0.0.1:54190 (509)。

最佳答案

请参阅setChannelCheckoutTimeout

    /**
     * Sets the channel checkout timeout. When greater than 0, enables channel limiting
     * in that the {@link #channelCacheSize} becomes the total number of available channels per
     * connection rather than a simple cache size. Note that changing the {@link #channelCacheSize}
     * does not affect the limit on existing connection(s), invoke {@link #destroy()} to cause a
     * new connection to be created with the new limit.
     * <p>
     * Since 1.5.5, also applies to getting a connection when the cache mode is CONNECTION.
     * @param channelCheckoutTimeout the timeout in milliseconds; default 0 (channel limiting not enabled).
     * @since 1.4.2
     * @see #setConnectionLimit(int)
     */
    public void setChannelCheckoutTimeout(long channelCheckoutTimeout) {

编辑

这按预期工作...

spring.rabbitmq.cache.channel.size=2
spring.rabbitmq.cache.channel.checkout-timeout=1s
spring.rabbitmq.publisher-confirm-type=correlated
spring.rabbitmq.publisher-returns=true
@SpringBootApplication
public class So69699961Application {

    public static void main(String[] args) {
        SpringApplication.run(So69699961Application.class, args).close();
    }

    @Bean
    ApplicationRunner runner(CachingConnectionFactory cf) {
        return args -> {
            Connection conn = cf.createConnection();
            Channel chann1 = conn.createChannel(false);
            Channel chann2 = conn.createChannel(false);
            try {
                Channel chann3 = conn.createChannel(false);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            chann2.close();
            chann1.close();
            conn.close();
        };
    }

}
org.springframework.amqp.AmqpTimeoutException: No available channels

关于spring-amqp - 有什么办法可以限制制作者创建的 channel 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69699961/

相关文章:

java - 将 spring-cloud 版本从 Camden.SR4 升级到 Camden.SR5 后,调度程序没有订阅者错误

java - spring rabbit amqp @RabbitListener 配置消费者的最小和最大数量

rabbitmq - 慢速网络中的大型 RabbitMQ 消息

spring - Spring AMQP 中的发布者返回是什么

spring-boot - Spring rabbit 重试传递被拒绝的消息..可以吗?

rabbitmq - Spring AMQP RabbitMQ - 直接回复的错误处理

spring-amqp - 我怎样才能得到 correlationId?

java - 为什么 DefaultJackson2JavaTypeMapper.toJavaType() 不支持抽象类和接口(interface)作为推断类型?

rabbitmq - Spring amqp 与 RabbitMq : Message is not circled back to live queue after falling off dead letter queue

java - 在 Spring 中以编程方式启用和禁用某些 @RabbitListener?