amq笔记:记一个关于PooledConnectionFactory的问题
替人排查一个关于amq连接数的问题,使用PooledConnectionFactory进行连接池管理,设置了连接数上限为3,但部署到服务器之后,瞬间建立了几百个连接,用netstat -an 查看,发现大部分到amq服务器的连接状态都是TIME_WAIT。
初始化连接池:
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); ActiveMQPrefetchPolicy activeMQPrefetchPolicy = new ActiveMQPrefetchPolicy(); activeMQConnectionFactory.setPrefetchPolicy(activeMQPrefetchPolicy); activeMQConnectionFactory.setUserName("XXX"); activeMQConnectionFactory.setPassword("XXXX"); activeMQConnectionFactory.setUseAsyncSend(true); activeMQConnectionFactory .setBrokerURL("failover:(tcp://XXXXX:XXXXX,tcp://XXXXX:XXXXX)?randomize=true"); PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory( activeMQConnectionFactory); pooledConnectionFactory.setMaximumActiveSessionPerConnection(10); pooledConnectionFactory.setMaxConnections(3);
发消息:
try { MessageProducer producer = null; connection = pooledConnectionFactory.getConnectionFactory().createConnection();//此句大雾 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session .createQueue("XXXXX"); producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); connection.start(); TextMessage message = session.createTextMessage(sMessage); producer.send(message); } finally { if (session != null) { session.close(); } if (connection != null) { connection.close(); } }
用尽各种关键词都找不到相似的问题描述,最后还是从代码下手,发现connection = pooledConnectionFactory.getConnectionFactory().createConnection();这一句用法比较怪异。最大连接数属性是针对pooledConnectionFactory设置的,pooledConnectionFactory本身有createConnection方法,为什么还要把里面的connectionFactory取出来再创建呢?
进代码里看,发现原来的代码引用的方法是取出连接池中的activeMQConnectionFactory后直接新建连接,调用close方法时关闭,根据TCP的工作原理,主动关闭的连接会保持一段时间的TIME_WAIT状态再行彻底关闭。而pooledConnectionFactory的createConnection才是在进行连接池管理。因此,只要把创建连接的那句代码改为connection = pooledConnectionFactory.createConnection();就可以解决这个问题。