java 垃圾收集器是否负责在 connection.close() 之后释放 JMS 连接

Is the java Garbage collector responsible to release the JMS connection after connection.close()

我正在使用 GlassFish JMS ConnectionFactory。最后关闭连接。最大池大小设置为 5。

测试用例: 我在 3 秒内从 invoker() 连续发送了 10 条消息。

结果:前5条消息发送成功,第6条消息之后分配更多连接失败。这意味着之前的所有 5 个连接仍处于打开状态。

问题一:connection.close()后多久释放连接poll?

问题2:JMSconnection.close()后是否由垃圾收集器负责释放连接?

这是我的简单消息客户端,它向队列发送消息

private void invoker(字符串 id){

    Connection connection = null;
    Session session = null;
    MessageProducer messageProducer = null;
    TextMessage message = null;
    try {
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        messageProducer = session.createProducer(successfulQueue);
        String msg = id;
        message = session.createTextMessage(msg);
        messageProducer.send(message);
        log.info("Successful message is Sent to ProcessQueue: [" + msg + "]");
    }
    catch (Exception e) {
        log.error(e);
    }
    finally {
        if (messageProducer != null) {
            try {
                messageProducer.close();
            }
            catch (JMSException e) {
                log.error(e);
            }
        }
        if (session != null) {
            try {
                session.close();
            }
            catch (JMSException e) {
                log.error(e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            }
            catch (JMSException e) {
                log.error(e);
            }
        }
    }

你的第二个问题的答案是 GC 没有关闭连接,而是 KeepsAlive 的后台进程,连接被 connection.close() 终止 (关于 GC 的工作原理,请阅读:https://www.quora.com/How-does-garbage-collection-work-in-the-JVM

建议:尝试使用 A PooledConnectionFactory 并在 connection.close

之外执行 .stop()
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    if (connection != null || pcf != null) try {
          connection.close();
    //pcf.stop();
    }
    catch (JMSException e) {
         //log
    }