java - 连接关闭不返回

标签 java connection jms

我有一个 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();
    }
}

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

When this method is invoked, it should not return until message processing has been shut down in an orderly fashion. This means that all message listeners that may have been running have returned, and that all pending receives have returned.

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

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

最佳答案

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

我看到很多用于停止操作的代码,但没有看到任何处理。

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

关于java - 连接关闭不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497293/

相关文章:

MySQL 本地主机 : "connection refused"

java - 如何可靠地发送 JMS 消息? (故障转移 MessageProducer.send() 错误)

node.js - 连接建立后如何运行 JavaScript 代码?

hadoop - 由于连接拒绝连接到端口49941, yarn 应用程序失败

java - JMS 使用哪种协议(protocol)来发送和接收消息?

java - WebSphere MQ JMS 连接发生错误

java - 如何使用 HTML 中的嵌入 Java 应用程序访问本地文件系统

java - 不使用maven默认布局有什么缺点?

java - Java 中的可视化继承

java - 从 JSON 转换为 JSON-LD