通过JMS API检查HornetQ中是否存在队列

时间:2014-09-03 11:12:46

标签: java jms hornetq

我想检查HornetQ中是否存在jms队列。我正在使用jms管理API:

public boolean queueExists(String queueName) throws Exception 
{
    final Queue managementQueue = HornetQJMSClient.createQueue("hornetq.management");
    Connection connection = jmsConnectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueRequestor requestor = new QueueRequestor((QueueSession) session, managementQueue);
    connection.start();
    Message message = session.createMessage();
    JMSManagementHelper.putAttribute(message, queueName, ???);
    Message reply = requestor.request(message);
    Object result = JMSManagementHelper.getResult(reply);
    //??? 
}

我的问题是我应该在请求消息中为此目的添加哪个属性?我在哪里可以找到所有可能的属性(文档或类的常量字段)?

JMSManagementHelper.putAttribute(message, queueName, ???);

我应该怎样处理回复消息的结果才能理解请求结果?

Message reply = requestor.request(message);
Object result = JMSManagementHelper.getResult(reply);
//??? 

我正在使用HornetQ 2.4.1.Final。

1 个答案:

答案 0 :(得分:1)

必须使用JMSManagementHelper.putOperationInvocation()而不是JMSManagementHelper.putAttribute(),资源名称为:jms和操作参数getQueueNames。此操作的结果是包含所有jms队列名称的Object []。

public boolean queueExists(String queueName) throws Exception 
{
    final Queue managementQueue = HornetQJMSClient.createQueue("hornetq.management");
    Connection connection = jmsConnectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueRequestor requestor = new QueueRequestor((QueueSession) session, managementQueue);
    connection.start();
    Message message = session.createMessage();
    JMSManagementHelper.putOperationInvocation(message,
       org.hornetq.api.core.management.ResourceNames.JMS_SERVER, "getQueueNames");
    Message reply = requestor.request(message);
    Object result = JMSManagementHelper.getResult(reply);
    Object[] results = (Object[])result;
    for (int i = 0; i < results.length; i++)  
    {  
        if (queueName.equals(((String)results[i])))  
            return true;  
    }
    return false;
}