title: Spring - Rabbitmq Listener needs to paused and resumed during database change
body: <p>Hey The requirement is to pause the rabbitmq listeners from processing messages during a change to the backend tables. This change is limited to just my application so don't want to bring down the entire rabbitmq instance. Once the process is complete I want to kickstart the listeners again. </p>
  
  <p><strong>Issue I'm facing</strong>
  I have 2 listeners connected to 2 separate queues sharing a 'consumerconnectionFactory'. When I killed the connection, only the one without any open channels get killed and when I resumed the connection I got an extra connection which was not there earlier. <strong>Can you please help.</strong></p>
  
  <p>I'm sharing my java configs below.</p>
  
  <pre><code>@Bean
      public SimpleMessageListenerContainer auditMessageListenerContainer(AuditMessageListener auditMessageListener)
      {
          SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(consumerConnectionFactory);
          container.setQueueNames(messagingAuditQueue);
          container.setMessageListener(auditMessageListener);
          container.setMaxConcurrentConsumers(5);
          container.setAcknowledgeMode(AcknowledgeMode.AUTO);
          container.setDefaultRequeueRejected(false);
          container.setMissingQueuesFatal(false);
          container.setForceCloseChannel(true);
          container.setExclusive(false);
          return container;
      }
      @Bean
      public SimpleMessageListenerContainer accessMessageListenerContainer(AccessLogListener accessLogListener)
      {
          SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(consumerConnectionFactory);
          container.setQueueNames(accessAuditQueue);
          container.setMessageListener(accessLogListener);
          container.setMaxConcurrentConsumers(5);
          container.setAcknowledgeMode(AcknowledgeMode.AUTO);
          container.setDefaultRequeueRejected(false);
          container.setMissingQueuesFatal(false);
          container.setForceCloseChannel(true);
          container.setExclusive(false);
          return container;
      }
  </code></pre>
  
  <p>This is how I did the Java config for the Listeners. </p>
  
  <p>Below is the RestController to start and stop the listeners</p>
  
  <pre><code>@RestController
  @RequestMapping(MESSAGE_AUDIT_ROOT)
  public class RestartController {
      @Autowired
      private List&lt;MessageListenerContainer&gt; listenerContainers;
  
      @Autowired
      private List&lt;ConnectionFactory&gt; connectionFactories;
  
      @GetMapping("/stop")
      public String stopMessageListenerContainer() {
          connectionFactories.forEach(conFactory -&gt; {
              CachingConnectionFactory cConFactory = (CachingConnectionFactory) conFactory;
              cConFactory.resetConnection();
          });
          listenerContainers.forEach(container -&gt; {
              SimpleMessageListenerContainer smlc = (SimpleMessageListenerContainer) container;
              smlc.shutdown();
          });
          listenerContainers.forEach(container -&gt; System.out
                  .println("Container: " + container.toString() + "is Running ?" + container.isRunning()));
          return "done - stop";
      }
  
      @GetMapping("/start")
      public String startMessageListenerContainer() {
          connectionFactories.forEach(conFactory -&gt; {
              CachingConnectionFactory cConFactory = (CachingConnectionFactory) conFactory;
              cConFactory.createConnection();
          });
          listenerContainers.forEach(container -&gt; {
              SimpleMessageListenerContainer smlc = (SimpleMessageListenerContainer) container;
              smlc.start();
          });
          listenerContainers.forEach(container -&gt; System.out
                  .println("Container: " + container.toString() + "is Running ?" + container.isRunning()));
          return "done - start";
      }
  
  }
  </code></pre>
  
  <p>Below is the Images for the behavior I see locally.
  1. Initial connections list
  <a href="https://i.stack.imgur.com/l910E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l910E.png" alt="Initial connections list"></a></p>
  
  <ol start="2">
  <li>When Connection stop Rest call
  <a href="https://i.stack.imgur.com/9vsif.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9vsif.png" alt="enter image description here"></a></li>
  </ol>
  
  <p>2.1 Queue connection still active
  <a href="https://i.stack.imgur.com/abCGe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/abCGe.png" alt="enter image description here"></a>
  3. When connection start Rest Call
  <a href="https://i.stack.imgur.com/hJTUf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hJTUf.png" alt="enter image description here"></a></p>