如何使用Java验证JMS队列是否存在? [英] How do I verify that a JMS queue exists using Java?
问题描述
如何使用Java API检查JMS服务器上是否存在队列?我现在不想向队列发送或接收任何数据,只需验证队列是否存在。此外,队列可能为空。
How can I check whether a queue exists on a JMS server using the Java API? I don't want to send or receive any data to the queue for now, just verify that the queue exists. Also, the queue may be empty.
这是我的代码示例。为简单起见,我删除了错误处理。
Here is my code sample. I have removed the error handling for simplicity.
Connection connection = null;
Session session = null;
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//I was hoping this next line would throw an exception if the queue does not exist
Queue queue = session.createQueue(queueName);
我的JMS服务器是TIBCO EMS。我希望有一个适用于5-7版本的解决方案。
My JMS server is TIBCO EMS. I'm hoping for a solution that works on versions 5-7.
解决方案
我按照接受的答案中的建议,但创建了一个浏览器。以下行根据需要引发了异常:
I followed the recommendation in the accepted answer but created a browser instead. The following line threw an exception as desired:
QueueBrowser browser = session.createBrowser(queue);
推荐答案
尝试在会话中传递消费者或生产者刚刚创建的队列对象:
Try creating a consumer or producer off the Session passing in the queue object you just created:
session.createConsumer(queue);
如果队列(或主题)不存在,这将抛出InvalidDestinationException。
This should throw an InvalidDestinationException if the queue (or topic) does not exist.
这篇关于如何使用Java验证JMS队列是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!