我正在开发一个系统集成主题的小型项目,并且正在使用JMS(JBOSS)。我们必须使用耐用的话题,而这一部分非常简单。假设我使用下面的代码:无法在持久订阅上创建订阅
TopicConnectionFactory topicConnectionFactory = InitialContext.doLookup("jms/RemoteConnectionFactory");
try(JMSContext jmsContext = topicConnectionFactory.createContext(<username>,<password>)) {
Topic topic = InitialContext.doLookup(<topic>);
JMSConsumer jmsConsumer = jmsContext.createDurableConsumer(topic, <client-id>);
Message message = jmsConsumer.receive();
if(message != null) {
result = message.getBody(ArrayList.class);
}
}
这种try-with-resources是有用的,因为它在块结束时破坏连接。但假设我在JMSConsumer等待消息时中断程序。当我重新启动该程序,它会抛出:
javax.jms.IllegalStateRuntimeException: Cannot create a subscriber on the durable subscription since it already has subscriber(s)
有没有办法关闭连接/取消/某物的程序被中断时?
你能抓住interruptedexception,做一些清理,然后重新抛出吗? –
我试着添加一个ShutdownHook,但它不起作用。我会再次检查文档,我可能会试图以错误的方式关闭连接,我不知道。 – Budgerous