diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java index cfde85ea4d..cbee288074 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ActiveMQServerControl.java @@ -904,6 +904,29 @@ public interface ActiveMQServerControl { @Operation(desc = "List the sessions for the given connectionID as a JSON string", impact = MBeanOperationInfo.INFO) String listSessionsAsJSON(@Parameter(desc = "a connection ID", name = "connectionID") String connectionID) throws Exception; + /** + * Lists details about all sessions. + * The returned String is a JSON string containing details about each and every session, e.g.: + *
+    * [
+    *   {
+    *     "sessionID":"e71d61d7-2176-11e8-9057-a0afbd82eaba",
+    *     "creationTime":1520365520212,
+    *     "consumerCount":1,
+    *     "principal":"myUser"
+    *   },
+    *   {
+    *     "sessionID":"e718a6e6-2176-11e8-9057-a0afbd82eaba",
+    *     "creationTime":1520365520191,
+    *     "consumerCount":0,
+    *     "principal":"guest"
+    *   }
+    * ]
+    * 
+ */ + @Operation(desc = "List all sessions as a JSON string", impact = MBeanOperationInfo.INFO) + String listAllSessionsAsJSON() throws Exception; + /** * Lists all the sessions IDs for the specified connection ID. */ diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java index d0f0b5cc63..fe7792b400 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java @@ -1863,6 +1863,30 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active return array.build().toString(); } + @Override + public String listAllSessionsAsJSON() throws Exception { + checkStarted(); + + clearIO(); + + JsonArrayBuilder array = JsonLoader.createArrayBuilder(); + try { + Set sessions = server.getSessions(); + for (ServerSession sess : sessions) { + JsonObjectBuilder obj = JsonLoader.createObjectBuilder().add("sessionID", sess.getName()).add("creationTime", sess.getCreationTime()).add("consumerCount", sess.getServerConsumers().size()); + + if (sess.getValidatedUser() != null) { + obj.add("principal", sess.getValidatedUser()); + } + + array.add(obj); + } + } finally { + blockOnIO(); + } + return array.build().toString(); + } + @Override public String listConsumersAsJSON(String connectionID) throws Exception { checkStarted(); diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java index 2185e07147..653b45c910 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java @@ -1472,6 +1472,52 @@ public class ActiveMQServerControlTest extends ManagementTestBase { Assert.assertEquals(1, second.getJsonNumber("consumerCount").longValue()); } + @Test + public void testListAllSessionsAsJSON() throws Exception { + SimpleString queueName = new SimpleString(UUID.randomUUID().toString()); + server.addAddressInfo(new AddressInfo(queueName, RoutingType.ANYCAST)); + server.createQueue(queueName, RoutingType.ANYCAST, queueName, null, false, false); + ActiveMQServerControl serverControl = createManagementControl(); + + ServerLocator locator = createInVMNonHALocator(); + ClientSessionFactory factory = createSessionFactory(locator); + ServerLocator locator2 = createInVMNonHALocator(); + ClientSessionFactory factory2 = createSessionFactory(locator2); + ClientSession session1 = addClientSession(factory.createSession()); + Thread.sleep(5); + ClientSession session2 = addClientSession(factory2.createSession("myUser", "myPass", false, false, false, false, 0)); + session2.createConsumer(queueName); + + String jsonString = serverControl.listAllSessionsAsJSON(); + IntegrationTestLogger.LOGGER.info(jsonString); + Assert.assertNotNull(jsonString); + JsonArray array = JsonUtil.readJsonArray(jsonString); + Assert.assertEquals(2 + (usingCore() ? 1 : 0), array.size()); + JsonObject first; + JsonObject second; + if (array.getJsonObject(0).getJsonNumber("creationTime").longValue() < array.getJsonObject(1).getJsonNumber("creationTime").longValue()) { + first = array.getJsonObject(0); + second = array.getJsonObject(1); + } else { + first = array.getJsonObject(1); + second = array.getJsonObject(0); + } + + Assert.assertTrue(first.getString("sessionID").length() > 0); + Assert.assertEquals(((ClientSessionImpl) session1).getName(), first.getString("sessionID")); + Assert.assertTrue(first.getString("principal").length() > 0); + Assert.assertEquals("guest", first.getString("principal")); + Assert.assertTrue(first.getJsonNumber("creationTime").longValue() > 0); + Assert.assertEquals(0, first.getJsonNumber("consumerCount").longValue()); + + Assert.assertTrue(second.getString("sessionID").length() > 0); + Assert.assertEquals(((ClientSessionImpl) session2).getName(), second.getString("sessionID")); + Assert.assertTrue(second.getString("principal").length() > 0); + Assert.assertEquals("myUser", second.getString("principal")); + Assert.assertTrue(second.getJsonNumber("creationTime").longValue() > 0); + Assert.assertEquals(1, second.getJsonNumber("consumerCount").longValue()); + } + @Test public void testListQueues() throws Exception { SimpleString queueName1 = new SimpleString("my_queue_one"); diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java index 57ead701ac..ce2e8cda1b 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlUsingCoreTest.java @@ -1002,6 +1002,11 @@ public class ActiveMQServerControlUsingCoreTest extends ActiveMQServerControlTes return (String) proxy.invokeOperation("listSessionsAsJSON", connectionID); } + @Override + public String listAllSessionsAsJSON() throws Exception { + return (String) proxy.invokeOperation("listAllSessionsAsJSON"); + } + @Override public String listAddresses(@Parameter(name = "separator", desc = "Separator used on the string listing") String separator) throws Exception { return (String) proxy.invokeOperation("listAddresses", separator);