[hornetq-commits] JBoss hornetq SVN: r10073 - in trunk: src/main/org/hornetq/jms/management/impl and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Dec 27 13:30:58 EST 2010


Author: clebert.suconic at jboss.com
Date: 2010-12-27 13:30:58 -0500 (Mon, 27 Dec 2010)
New Revision: 10073

Modified:
   trunk/src/main/org/hornetq/api/jms/management/JMSServerControl.java
   trunk/src/main/org/hornetq/jms/management/impl/JMSServerControlImpl.java
   trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlTest.java
   trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlUsingJMSTest.java
Log:
HORNETQ-587 - adding listAllConsumersAsJSON method

Modified: trunk/src/main/org/hornetq/api/jms/management/JMSServerControl.java
===================================================================
--- trunk/src/main/org/hornetq/api/jms/management/JMSServerControl.java	2010-12-27 17:51:48 UTC (rev 10072)
+++ trunk/src/main/org/hornetq/api/jms/management/JMSServerControl.java	2010-12-27 18:30:58 UTC (rev 10073)
@@ -214,6 +214,15 @@
    String listConsumersAsJSON(@Parameter(desc = "a connection ID", name = "connectionID") String connectionID) throws Exception;
 
    /**
+    * Lists all the consumers
+    * The returned String is a JSON string containing an array of JMSConsumerInfo objects.
+    * 
+    * @see JMSConsumerInfo#from(String)
+    */
+   @Operation(desc = "List all JMS consumers associated to a JMS Connection")
+   String listAllConsumersAsJSON() throws Exception;
+
+   /**
     * Lists all addresses to which the designated server session has sent messages.
     */
    @Operation(desc = "Lists all addresses to which the designated session has sent messages", impact = MBeanOperationInfo.INFO)

Modified: trunk/src/main/org/hornetq/jms/management/impl/JMSServerControlImpl.java
===================================================================
--- trunk/src/main/org/hornetq/jms/management/impl/JMSServerControlImpl.java	2010-12-27 17:51:48 UTC (rev 10072)
+++ trunk/src/main/org/hornetq/jms/management/impl/JMSServerControlImpl.java	2010-12-27 18:30:58 UTC (rev 10073)
@@ -170,17 +170,17 @@
 
       try
       {
-         if(useDiscovery)
+         if (useDiscovery)
          {
-            if(connectorNames == null || connectorNames.length == 0)
+            if (connectorNames == null || connectorNames.length == 0)
             {
                throw new IllegalArgumentException("no discovery group name supplied");
             }
             server.createConnectionFactory(name,
-                                        ha,
-                                        JMSFactoryType.valueOf(cfType),
-                                        connectorNames[0],
-                                        JMSServerControlImpl.convert(bindings));
+                                           ha,
+                                           JMSFactoryType.valueOf(cfType),
+                                           connectorNames[0],
+                                           JMSServerControlImpl.convert(bindings));
          }
          else
          {
@@ -192,13 +192,12 @@
             }
 
             server.createConnectionFactory(name,
-                  ha,
-                  JMSFactoryType.valueOf(cfType),
-                  connectorList,
-                  JMSServerControlImpl.convert(bindings));
+                                           ha,
+                                           JMSFactoryType.valueOf(cfType),
+                                           connectorList,
+                                           JMSServerControlImpl.convert(bindings));
          }
 
-
          sendNotification(NotificationType.CONNECTION_FACTORY_CREATED, name);
       }
       finally
@@ -213,7 +212,12 @@
     * The ConnectionFactory is bound to JNDI for all the specified bindings Strings.
     *  
     */
-   public void createConnectionFactory(String name, boolean ha, boolean useDiscovery, int cfType, String connectors, String jndiBindings) throws Exception
+   public void createConnectionFactory(String name,
+                                       boolean ha,
+                                       boolean useDiscovery,
+                                       int cfType,
+                                       String connectors,
+                                       String jndiBindings) throws Exception
    {
       createConnectionFactory(name, ha, useDiscovery, cfType, toArray(connectors), toArray(jndiBindings));
    }
@@ -584,44 +588,11 @@
                   Set<ServerConsumer> consumers = session.getServerConsumers();
                   for (ServerConsumer consumer : consumers)
                   {
-                     JSONObject obj = new JSONObject();
-                     obj.put("consumerID", consumer.getID());
-                     obj.put("connectionID", connectionID);
-                     obj.put("queueName", consumer.getQueue().getName().toString());
-                     obj.put("browseOnly", consumer.isBrowseOnly());
-                     obj.put("creationTime", consumer.getCreationTime());
-                     // JMS consumer with message filter use the queue's filter
-                     Filter queueFilter = consumer.getQueue().getFilter();
-                     if (queueFilter != null)
+                     JSONObject obj = toJSONObject(consumer);
+                     if (obj != null)
                      {
-                        obj.put("filter", queueFilter.getFilterString().toString());
+                        array.put(obj);
                      }
-                     String[] destinationInfo = determineJMSDestination(consumer.getQueue().getAddress().toString());
-                     if (destinationInfo == null)
-                     {
-                        continue;
-                     }
-                     obj.put("destinationName", destinationInfo[0]);
-                     obj.put("destinationType", destinationInfo[1]);
-                     if (destinationInfo[1].equals("topic"))
-                     {
-                        try
-                        {
-                           HornetQDestination.decomposeQueueNameForDurableSubscription(consumer.getQueue()
-                                                                                               .getName()
-                                                                                               .toString());
-                           obj.put("durable", true);
-                        }
-                        catch (IllegalArgumentException e)
-                        {
-                           obj.put("durable", false);
-                        }
-                     }
-                     else
-                     {
-                        obj.put("durable", false);
-                     }
-                     array.put(obj);
                   }
                }
             }
@@ -634,6 +605,37 @@
       }
    }
 
+   public String listAllConsumersAsJSON() throws Exception
+   {
+      checkStarted();
+
+      clearIO();
+
+      try
+      {
+         JSONArray array = new JSONArray();
+
+         Set<ServerSession> sessions = server.getHornetQServer().getSessions();
+         for (ServerSession session : sessions)
+         {
+            Set<ServerConsumer> consumers = session.getServerConsumers();
+            for (ServerConsumer consumer : consumers)
+            {
+               JSONObject obj = toJSONObject(consumer);
+               if (obj != null)
+               {
+                  array.put(obj);
+               }
+            }
+         }
+         return array.toString();
+      }
+      finally
+      {
+         blockOnIO();
+      }
+   }
+
    public String[] listSessions(final String connectionID) throws Exception
    {
       checkStarted();
@@ -844,4 +846,45 @@
       return array.toString();
    }
 
+   private JSONObject toJSONObject(ServerConsumer consumer) throws Exception
+   {
+      JSONObject obj = new JSONObject();
+      obj.put("consumerID", consumer.getID());
+      obj.put("connectionID", consumer.getConnectionID());
+      obj.put("queueName", consumer.getQueue().getName().toString());
+      obj.put("browseOnly", consumer.isBrowseOnly());
+      obj.put("creationTime", consumer.getCreationTime());
+      // JMS consumer with message filter use the queue's filter
+      Filter queueFilter = consumer.getQueue().getFilter();
+      if (queueFilter != null)
+      {
+         obj.put("filter", queueFilter.getFilterString().toString());
+      }
+      String[] destinationInfo = determineJMSDestination(consumer.getQueue().getAddress().toString());
+      if (destinationInfo == null)
+      {
+         return null;
+      }
+      obj.put("destinationName", destinationInfo[0]);
+      obj.put("destinationType", destinationInfo[1]);
+      if (destinationInfo[1].equals("topic"))
+      {
+         try
+         {
+            HornetQDestination.decomposeQueueNameForDurableSubscription(consumer.getQueue().getName().toString());
+            obj.put("durable", true);
+         }
+         catch (IllegalArgumentException e)
+         {
+            obj.put("durable", false);
+         }
+      }
+      else
+      {
+         obj.put("durable", false);
+      }
+
+      return obj;
+   }
+
 }

Modified: trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlTest.java	2010-12-27 17:51:48 UTC (rev 10072)
+++ trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlTest.java	2010-12-27 18:30:58 UTC (rev 10073)
@@ -23,6 +23,7 @@
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
+import javax.jms.MessageConsumer;
 import javax.jms.MessageProducer;
 import javax.jms.Queue;
 import javax.jms.Session;
@@ -65,6 +66,7 @@
 import org.hornetq.tests.unit.util.InVMContext;
 import org.hornetq.tests.util.RandomUtil;
 import org.hornetq.tests.util.UnitTestCase;
+import org.hornetq.utils.json.JSONArray;
 
 /**
  * A JMSServerControlTest
@@ -387,6 +389,56 @@
       Assert.assertNull(fakeJMSStorageManager.destinationMap.get(topicName));
    }
 
+
+   public void testListAllConsumers() throws Exception
+   {
+      String topicJNDIBinding = RandomUtil.randomString();
+      String topicName = RandomUtil.randomString();
+
+      UnitTestCase.checkNoBinding(context, topicJNDIBinding);
+      checkNoResource(ObjectNameBuilder.DEFAULT.getJMSTopicObjectName(topicName));
+
+      JMSServerControl control = createManagementControl();
+      control.createTopic(topicName, topicJNDIBinding);
+
+      checkResource(ObjectNameBuilder.DEFAULT.getJMSTopicObjectName(topicName));
+      Topic topic = (Topic)context.lookup(topicJNDIBinding);
+      assertNotNull(topic);
+      HornetQConnectionFactory cf = new HornetQConnectionFactory(false,
+                                                                 new TransportConfiguration(InVMConnectorFactory.class.getName()));
+      Connection connection = cf.createConnection();
+      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      // create a consumer will create a Core queue bound to the topic address
+      MessageConsumer cons = session.createConsumer(topic);
+      
+      System.out.println("jsonString:" + control.listAllConsumersAsJSON());
+      JSONArray jsonArray = new JSONArray(control.listAllConsumersAsJSON());
+      
+      assertEquals(1, jsonArray.length());
+      
+      cons.close();
+      
+      jsonArray = new JSONArray(control.listAllConsumersAsJSON());
+      
+      assertEquals(0, jsonArray.length());
+
+      String topicAddress = HornetQDestination.createTopicAddressFromName(topicName).toString();
+      AddressControl addressControl = (AddressControl)server.getManagementService()
+                                                            .getResource(ResourceNames.CORE_ADDRESS + topicAddress);
+      assertNotNull(addressControl);
+
+      assertTrue(addressControl.getQueueNames().length > 0);
+
+      connection.close();
+      control.destroyTopic(topicName);
+
+      assertNull(server.getManagementService().getResource(ResourceNames.CORE_ADDRESS + topicAddress));
+      UnitTestCase.checkNoBinding(context, topicJNDIBinding);
+      checkNoResource(ObjectNameBuilder.DEFAULT.getJMSTopicObjectName(topicName));
+
+      Assert.assertNull(fakeJMSStorageManager.destinationMap.get(topicName));
+   }
+
    public void testGetTopicNames() throws Exception
    {
       String topicJNDIBinding = RandomUtil.randomString();

Modified: trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlUsingJMSTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlUsingJMSTest.java	2010-12-27 17:51:48 UTC (rev 10072)
+++ trunk/tests/src/org/hornetq/tests/integration/jms/server/management/JMSServerControlUsingJMSTest.java	2010-12-27 18:30:58 UTC (rev 10073)
@@ -261,7 +261,12 @@
             proxy.invokeOperation("createConnectionFactory", name, ha, useDiscovery, cfType, connectors, jndiBindings);
          }
 
+         public String listAllConsumersAsJSON() throws Exception
+         {
+            return (String)proxy.invokeOperation("listAllConsumersAsJSON");
+         }
 
+
       };
    }
    // Public --------------------------------------------------------



More information about the hornetq-commits mailing list