【发布时间】: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