title: How to close connection with Rabbit MQ broker using spring-amqp
body: <p>We have been struggling with this for sometime now. Below is our configuration:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  
         xsi:schemaLocation="http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.1.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd" xmlns:context="http://www.springframework.org/schema/context"&gt;
  
  &lt;context:property-placeholder location="../spring.properties" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactory" host="${app.mq.host}" username="${app.mq.username}" 
                             password="${app.mq.password}" virtual-host="${app.mq.virtualhost}" port="${app.mq.port}"/&gt; 
  
  &lt;rabbit:connection-factory id="connectionFactorySettlement" host="${app.mq.host}" username="${app.mq.username}" 
                             password="${app.mq.password}" virtual-host="${app.mq.settlementVirtualHost}" port="${app.mq.port}"/&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryAuth" host="${app.mq.host}" username="${app.mq.username}" 
                             password="${app.mq.password}" virtual-host="${app.mq.oAuthVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryVault" host="${app.mq.host}" username="${app.mq.username}" 
                             password="${app.mq.password}" virtual-host="${app.mq.vaultVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryWebservice" host="${app.mq.host}" username="${app.mq.username}" 
                             password="${app.mq.password}" virtual-host="${app.mq.webserviceVirtualHost}" port="${app.mq.port}" /&gt; 
  
  &lt;rabbit:connection-factory id="connectionFactoryCMS" host="${app.mq.host}" username="${app.mq.username}"
                             password="${app.mq.password}" virtual-host="${app.mq.cmsVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryTreasury" host="${app.mq.host}" username="${app.mq.username}"
                             password="${app.mq.password}" virtual-host="${app.mq.treasuryVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryBankVault" host="${app.mq.host}" username="${app.mq.username}"
                             password="${app.mq.password}" virtual-host="${app.mq.bankVaultVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryAnalyticsData" host="${app.mq.analytics_host}" username="${app.mq.analytics_username}"
                                 password="${app.mq.analytics_password}" virtual-host="${app.mq.analyticsdataVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryChat" host="${app.mq.host}" username="${app.mq.username}"
                                 password="${app.mq.password}" virtual-host="${app.mq.chatVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:connection-factory id="connectionFactoryFittipaldi" host="${app.mq.fittipaldi_host}" username="${app.mq.fittipaldi_username}"
                              password="${app.mq.fittipaldi_password}" virtual-host="${app.mq.fittipaldiVirtualHost}" port="${app.mq.port}" /&gt;
  
  &lt;rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateSettlement" connection-factory="connectionFactorySettlement" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateCMS" connection-factory="connectionFactoryCMS" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateVault" connection-factory="connectionFactoryVault" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateTreasury" connection-factory="connectionFactoryTreasury" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateWebservice" connection-factory="connectionFactoryWebservice" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateBankVault" connection-factory="connectionFactoryBankVault" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateAnalyticsData" connection-factory="connectionFactoryAnalyticsData" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateChat" connection-factory="connectionFactoryChat" reply-timeout="10" /&gt;
  &lt;rabbit:template id="rabbitTemplateAuth" connection-factory="connectionFactoryAuth" reply-timeout="1" /&gt;
  &lt;rabbit:template id="rabbitTemplateFitti" connection-factory="connectionFactoryFittipaldi" reply-timeout="5" /&gt;
  
  &lt;rabbit:admin connection-factory="connectionFactory" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>When we try to access the connection factory from java code and execute the destroy method, the connections do not close. Moreover when we inspect the open connections for that connection factory it shows no open connections but rabbitmq in practice has one open connection.</p>
  
  <pre><code>RabbitTemplate rabbitTemplate = (RabbitTemplate) rabbitTemplateMap.get(String.valueOf(key));
  CachingConnectionFactory cachingConnectionFactory = (CachingConnectionFactory) rabbitTemplate.getConnectionFactory();
  cachingConnectionFactory.destroy();
  </code></pre>
  
  <p>The above code snippet does not succeed in closing the connections. We are using the following dependency:</p>
  
  <pre><code>&lt;dependency&gt;
       &lt;groupId&gt;org.springframework.amqp&lt;/groupId&gt;
       &lt;artifactId&gt;spring-rabbit&lt;/artifactId&gt;
       &lt;version&gt;1.3.9.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>This requirement arises because of a peculiar situation wherein when the connections get blocked we want to provide for an option to shift to a substitute broker. Hence we have to close the current connections and establish new connections with the substitute broker. Please let me know if you need more information.</p>