0

I am using Spring Boot. I am trying to remove channels which are not in use using Spring AMQP (RabbitMQ) by Spring Boot. But not getting how to achieve it. Any help is appreciable.

ConnectionFactory Declaration:

public ConnectionFactory connectionFactory() {
        final CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(centralHost);
        connectionFactory.setHost(rabbitMqHost);
        connectionFactory.setConnectionTimeout(connectionTimeout);
        connectionFactory.setChannelCacheSize(4);
        connectionFactory.setExecutor(Executors.newFixedThreadPool(rabbitmqThreads));
        
        return connectionFactory;
    }

ContainerFactory Declaration:

public DirectRabbitListenerContainerFactory rabbitDirectListenerContainerFactory(ConnectionFactory connectionFactory) {
        DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.NONE);
        factory.setAfterReceivePostProcessors( m -> {
            m.getMessageProperties().setContentType("text/plain");
            return m;
        });
        return factory;
    }
ram m
  • 3
  • 4
  • What do you mean by `remove channels which are not in use` ? – Gary Russell Nov 16 '20 at 15:30
  • i mean to remove the channel which are got created in rabbitmq when queues are created. Here i'm deleting queues using AmqpAdmin class. Even after deleting the queues the channel is remaining as is. – ram m Nov 16 '20 at 16:21

1 Answers1

1

Why do you want to close it? That's the whole point of caching the channel(s); so we don't have to create a new one each time we publish a message.

The minimum cache size is 1.

You can call resetConnection() on the CachingConnectionFactory to close the connection and all cached channels.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Hi Gary, Could you please suggest me on this https://stackoverflow.com/questions/73580523/methodchannel-closereply-code-406-reply-text-precondition-failed-delivery – JDGuide Sep 12 '22 at 11:21