gpt4 book ai didi

java - 使用 CachingConnectionFactory 时未清理生产者

转载 作者:行者123 更新时间:2023-12-01 12:38:55 24 4
gpt4 key购买 nike

似乎JMSProducer没有进行垃圾收集,并且在将消息传递到队列后保持 Activity 状态,我正在使用Spring 3.2.2和带有Keep-alive设置的CachingConnectionFactory用于发送消息。

每次我发送消息时,生产者数量都会不断增加。

与我使用的spring版本有关吗?
或者我的配置做错了什么?

最佳答案

您需要调用 close() method在您的MessageProducer上。根据 Java 文档:-

void close()  
throws JMSException

Closes the message producer.

Since a provider may allocate some resources on behalf of a MessageProducer outside the Java virtual machine, clients should close them when they are not needed. Relying on garbage collection to eventually reclaim these resources may not be timely enough.

根据 Spring CachingConnectionFactory 文档:-

NOTE: This ConnectionFactory requires explicit closing of all Sessions obtained from its shared Connection. This is the usual recommendation for native JMS access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Session reuse.

因此,您需要调用 getCachedSessionProxy 而不是 getSession ,并在发送消息后调用 close() (在 finally block 中)。根据源代码,处理对此 session 代理的关闭调用,以便重用 session 和消息生产者。加里的评论也是如此。

关于java - 使用 CachingConnectionFactory 时未清理生产者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25306857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com