我们使用AMQ代理5.5和Spring 3.0来配置连接工厂和其他东西 . 我们使用的连接工厂是PooledConnectionFactory,我的配置的一部分如下所示:
<bean id =“jmsFactory”class =“org.apache.activemq.pool.PooledConnectionFactory”destroy-method =“stop”>
<property name =“connectionFactory”>
<bean class =“org.apache.activemq.ActiveMQConnectionFactory”> <property name =“brokerURL”value =“some_url”/>
</ beans >
</属性>
</ beans >
<! - Spring JMS模板 - >
<bean id =“jmsTemplate”class =“org.springframework.jms.core.JmsTemplate”>
<property name =“connectionFactory”> <ref local =“jmsFactory”/>
</属性>
<property name =“explicitQosEnabled”value =“true”/>
<property name =“timeToLive”value =“86400000”/>
</ beans
几天前,我们的经纪人崩溃并继续重新启动此错误:
java.lang.OutOfMemoryError:已请求
Chunk :: new的369384字节 . 交换空间?
在那个时间点,从jconsole,我找不到与代理有任何异常,除了我们的一个客户端应用程序通过每分钟发送和收听消息与服务器(通过代理)进行通信已经创建了~3000个连接(锯它在jconsole上) . 一旦我们关闭它,一切都恢复正常 .
所以,为了避免这种情况,我尝试在finally块中关闭连接,做这样的事情 .
try {
connection = myJmsTemplate.getConnectionFactory().createConnection();
session = connection.createSession(false, 1);
String messageSelector = "JMSCorrelationID='" + correlationId + "'";
responseConsumer = session.createConsumer(receiveDestination, messageSelector);
LOG.info("Starting connection");
connection.start();
myJmsTemplate.send(sendDestination, new SimpleTextMessageCreator(
message, receiveDestination, correlationId));
LOG.info("Waiting for message with " + messageSelector + " for " + DEFAULT_TIMEOUT + " ms");
TextMessage responseMessage = (TextMessage) responseConsumer.receive(DEFAULT_TIMEOUT);
}
catch (Someexception e) {do something}
finally {
responseConsumer.close();
session.close();
connection.close();
}
但即使这样,我也可以看到jconsole中的连接浮动,只有在发布消息的客户端应用程序被关闭时才会丢失 . 有人可以帮助我了解这里发生了什么以及如何在每个pub子周期后关闭连接 .
先感谢您,
哈日
1 回答
误报 . 还有另一段代码让连接保持打开状态 . 关闭它解决了这个问题 .