【问题标题】:Connection close not returning连接关闭不返回
【发布时间】:2012-02-29 10:21:25
【问题描述】:

我有一个 JMS 客户端,它使用以下代码从队列中消费消息:

    try {

        ...

        FileUtils.writeStringToFile(getNextFile(), txtMessage.getText());                   

    } catch (JMSException e) {
        LOG.error(e.getMessage());          
    } catch (IOException e) {
        LOG.error(e.getMessage());          
    }finally{
        if(this.openMessages<=0){
            try {
                if(transacted){
                    session.commit();
                    LOG.info("Session commited");
                }

                consumer.close();           

//              System.exit(0);             
                closeSession();                 

                System.exit(0);
            } catch (JMSException e) {
                LOG.error("Error while closing the consumer and session.", e);
            } catch (Exception e2) {
                LOG.error("Global Exception while closing the consumer and session.", e2);
            }           
        }           
    }

public void closeSession() throws JMSException {
    if (connection != null) {
        connection.stop();
        connection.close();
    }
}

关闭消费者后,我想关闭会话。为了关闭它,它调用了一个方法调用closeSession,它会停止连接并关闭它。不幸的是,由于某种原因,有时调用close 不会返回。因此,我在上面的代码 sn-p 中添加了 exit 语句(现已注释)。按照我们拥有的接口文档:

当调用此方法时,它不应返回,直到消息处理以有序的方式关闭。这意味着所有可能正在运行的消息侦听器都已返回,并且所有待处理的接收都已返回。

这不是我的情况,因为我知道所有消息都已处理完毕。

任何提示为什么这有时不起作用?

【问题讨论】:

  • close() 阻塞时可以进行线程转储吗? jstack/jconsole 或 JVisualVM 会这样做。

标签: java connection jms


【解决方案1】:

我认为没有理由在您的代码中调用 System.exit(0)

我看到很多停止内容的代码,但我没有看到任何处理。

你想多了。与我过去的做法相比,您的 MessageListener 看起来太复杂了。我会简化。侦听器不必消失并核对运行它的 JVM。它所要做的就是完成,关闭其连接,然后让应用服务器将其放回池中以等待下一条消息到达队列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多