我有这个方法,当数据队列不存在时会抛出异常,但实际上并不存在。你还有其他办法解决这个问题吗?
public void checkDataQueue(String dataQueue) throws JMSException {
Connection connection = null;
Session session = null;
connection = jmsTemplate.getConnectionFactory().createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(dataQueue);
QueueBrowser browser = session.createBrowser(queue);
}
ActiveMQ 5.x 默认情况下按需创建队列,因此您可能已更改默认配置以禁止这样做,在这种情况下,如果您遇到不存在的队列,则应该会发生错误,您应该检查并处理该错误。如果您需要确定,那么经纪商会提供一个 JMX 接口来查询有关经纪商统计信息等的信息。还有其他监控方式,例如通过 Jolokia 管理界面使用 Rest 风格调用。
我用这个解决了它:
public boolean existDataQueue(String dataQueue) throws JMSException {
boolean response = false;
ActiveMQConnectionFactory activeMQConnectionFactory =
new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerUrl);
ActiveMQConnection connection = (ActiveMQConnection)activeMQConnectionFactory.createConnection();
connection.start();
DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();
for (ActiveMQQueue activeMQQueue : queues) {
try {
if(activeMQQueue.getQueueName().equalsIgnoreCase(dataQueue)) {
response = true;
}
} catch (JMSException e) {
e.printStackTrace();
}
}
connection.close();
return response;
}