我们一直在努力解决这个问题。以下是我们的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<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">
<context:property-placeholder location="../spring.properties" />
<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}"/>
<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}"/>
<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}" />
<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}" />
<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}" />
<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}" />
<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}" />
<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}" />
<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}" />
<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}" />
<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}" />
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" reply-timeout="10" />
<rabbit:template id="rabbitTemplateSettlement" connection-factory="connectionFactorySettlement" reply-timeout="10" />
<rabbit:template id="rabbitTemplateCMS" connection-factory="connectionFactoryCMS" reply-timeout="10" />
<rabbit:template id="rabbitTemplateVault" connection-factory="connectionFactoryVault" reply-timeout="10" />
<rabbit:template id="rabbitTemplateTreasury" connection-factory="connectionFactoryTreasury" reply-timeout="10" />
<rabbit:template id="rabbitTemplateWebservice" connection-factory="connectionFactoryWebservice" reply-timeout="10" />
<rabbit:template id="rabbitTemplateBankVault" connection-factory="connectionFactoryBankVault" reply-timeout="10" />
<rabbit:template id="rabbitTemplateAnalyticsData" connection-factory="connectionFactoryAnalyticsData" reply-timeout="10" />
<rabbit:template id="rabbitTemplateChat" connection-factory="connectionFactoryChat" reply-timeout="10" />
<rabbit:template id="rabbitTemplateAuth" connection-factory="connectionFactoryAuth" reply-timeout="1" />
<rabbit:template id="rabbitTemplateFitti" connection-factory="connectionFactoryFittipaldi" reply-timeout="5" />
<rabbit:admin connection-factory="connectionFactory" />
</beans>
当我们尝试从java代码访问连接工厂并执行destroy方法时,连接不会关闭。此外,当我们检查该连接工厂的打开连接时,它显示没有打开的连接,但实际上rabbitmq有一个打开的连接。
RabbitTemplate rabbitTemplate = (RabbitTemplate) rabbitTemplateMap.get(String.valueOf(key));
CachingConnectionFactory cachingConnectionFactory = (CachingConnectionFactory) rabbitTemplate.getConnectionFactory();
cachingConnectionFactory.destroy();
以上代码段无法成功关闭连接。我们使用以下依赖项:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.3.9.RELEASE</version>
</dependency>
出现这种要求是因为一种特殊的情况,当连接被阻塞时,我们希望提供转换到替代经纪人的选项。因此,我们必须关闭当前连接并与替代代理建立新连接。如果您需要更多信息,请与我们联系。
答案 0 :(得分:2)
1.3.9是一个相当旧的版本(当前版本是1.6.1)。
但是,我不知道使用destroy()
方法关闭连接有任何问题。在1.5中,我们添加了resetConnection()
,其名称不太可怕,但与destroy()
的内容相同。
如果您有任何侦听器容器,重置后将立即重新建立连接;同样,如果使用模板发布消息,则将重新创建连接。
如果您仍然觉得有问题,我建议您使用wirehark或tcpdump等网络监视器来查看正在发生的事情。