1 Reply Latest reply on Sep 3, 2014 1:20 PM by Justin Bertram

    Checking if a queue exists in HornetQ via JMS API

    m f Newbie

      Hi,

      I want to check if a jms queue exists in HornetQ or not. I'm using jms management 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);
        //???
      }
      
      


      My Question is which attribute should i put in the request message for this purpose? And where can i find all possible attributes (documentation or constant fields of a class) ?


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


      And what should I do with the result of reply message to understand the request result?


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


      I'm using HornetQ 2.4.1.Final.

      Thanks in advance.

        • 1. Re: Checking if a queue exists in HornetQ via JMS API
          Justin Bertram Master

          To find out if a queue exists or not use the getQueueNames operation on org.hornetq.api.jms.management.JMSServerControl and then loop through the names to see if the one you're looking for exists.  It returns a String[].

           

          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(m, org.hornetq.api.core.management.ResourceNames.JMS_SERVER, "getQueueNames");
            Message reply = requestor.request(message);  
            Object[] results = (Object[])JMSManagementHelper.getResult(reply);
            for (int i = 0; i < results.length; i++)
            {
              System.out.println("Queue name: " + results[i]);
             if (queueName.equals(((String)results[i])))
               return true;
            }
          
            return false;
          }
          

           

          There's not a lot of documentation around these individual operations.  There's a JIRA open for this already.