繁体   English   中英

是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个连接仍处于打开状态。

问题1:在connection.close()之后释放连接轮询需要多长时间?

问题2:垃圾收集器是否负责在JMS connection.close()之后释放连接?

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

私有void调用程序(字符串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不会关闭连接,而是由keep.Alive进行的后台进程由connection.close()终止(有关GC的工作方式,请阅读: https ://www.quora.com/How- 在JVM中进行垃圾收集工作吗?

建议:尝试使用PooledConnectionFactory并在与connection.close相同的位置执行.stop()

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM