簡體   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