我有一个 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/