读书人

JmsTemplate 集成activemq中 connecti

发布时间: 2014-01-15 15:40:23 作者: rapoo

JmsTemplate 集成activemq中 connection 与 session的管理
1.PooledConnectionFactory中有段代码
(1) this.connectionsPool.setMaxIdle(1);
保证connectionPool只反回同一个连接

(2)
try {
connection = connectionsPool.borrowObject(key);
} catch (Exception e) {
throw JMSExceptionSupport.create("Error while attempting to retrieve a connection from the pool", e);
}

try {
connectionsPool.returnObject(key, connection);
} catch (Exception e) {
throw JMSExceptionSupport.create("Error when returning connection to the pool", e);
}

连接borrow出去时,立即return归还。
这样每个次发送,jmsTemplate.send ....方法,使终获取的同一个连接。

2.ConnectionPool中的
public Session createSession(boolean transacted, int ackMode) throws JMSException {
SessionKey key = new SessionKey(transacted, ackMode);
PooledSession session;
try {
session = sessionPool.borrowObject(key);
} catch (Exception e) {
throw JMSExceptionSupport.create(e);
}
return session;
}

在哪里归还的呢?是在PooledSession的close方法进行归还

3.注意在browsConnection时执行validateObject方法
中的connection.expiredCheck()。
过期时 sessionPool.close();

所以每session中如果没有过期的话,
这样每个次发送,jmsTemplate.send ....方法,使终获取的同一个连接且同一个session

读书人网 >软件架构设计

热点推荐