From 665a69886e2f09778df1fb14c1f889e68ba71a40 Mon Sep 17 00:00:00 2001 From: yang wei Date: Wed, 22 May 2019 20:37:35 +0800 Subject: [PATCH] ARTEMIS-2350 Fix ClassNotFoundException while invoking ActiveMQServerControlImpl::listConsumers --- .../activemq/artemis/api/core/JsonUtil.java | 5 +- tests/smoke-tests/pom.xml | 18 +++ .../smoke/jmx2/JmxServerControlTest.java | 107 ++++++++++++++++++ 3 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmx2/JmxServerControlTest.java diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java index eed944f29b..bd8b8dff12 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java @@ -16,7 +16,6 @@ */ package org.apache.activemq.artemis.api.core; -import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonArrayBuilder; import javax.json.JsonNumber; @@ -275,11 +274,11 @@ public final class JsonUtil { } public static JsonArray readJsonArray(String jsonString) { - return Json.createReader(new StringReader(jsonString)).readArray(); + return JsonLoader.createReader(new StringReader(jsonString)).readArray(); } public static JsonObject readJsonObject(String jsonString) { - return Json.createReader(new StringReader(jsonString)).readObject(); + return JsonLoader.createReader(new StringReader(jsonString)).readObject(); } public static Map readJsonProperties(String jsonString) { diff --git a/tests/smoke-tests/pom.xml b/tests/smoke-tests/pom.xml index 5caef58a64..b8885af29b 100644 --- a/tests/smoke-tests/pom.xml +++ b/tests/smoke-tests/pom.xml @@ -229,6 +229,24 @@ + + test-compile + create-createJMX2 + + create + + + true + admin + admin + ${basedir}/target/jmx2 + + + --java-options + -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=11099 -Dcom.sun.management.jmxremote.rmi.port=11098 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false + + + diff --git a/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmx2/JmxServerControlTest.java b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmx2/JmxServerControlTest.java new file mode 100644 index 0000000000..b0abf591f5 --- /dev/null +++ b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmx2/JmxServerControlTest.java @@ -0,0 +1,107 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.activemq.artemis.tests.smoke.jmx2; + +import javax.jms.Session; +import javax.json.JsonArray; +import javax.json.JsonObject; +import javax.management.MBeanServerConnection; +import javax.management.MBeanServerInvocationHandler; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; + +import com.google.common.collect.ImmutableMap; +import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration; +import org.apache.activemq.artemis.api.core.JsonUtil; +import org.apache.activemq.artemis.api.core.RoutingType; +import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl; +import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder; +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; +import org.apache.activemq.artemis.jms.client.ActiveMQQueue; +import org.apache.activemq.artemis.tests.smoke.common.SmokeTestBase; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class JmxServerControlTest extends SmokeTestBase { + // This test will use a smoke created by the pom on this project (smoke-tsts) + + private static final String JMX_SERVER_HOSTNAME = "localhost"; + private static final int JMX_SERVER_PORT = 11099; + + public static final String SERVER_NAME_0 = "jmx2"; + + @Before + public void before() throws Exception { + cleanupData(SERVER_NAME_0); + disableCheckThread(); + startServer(SERVER_NAME_0, 0, 30000); + } + + @Test + public void testListConsumers() throws Exception { + // Without this, the RMI server would bind to the default interface IP (the user's local IP mostly) + System.setProperty("java.rmi.server.hostname", JMX_SERVER_HOSTNAME); + + // I don't specify both ports here manually on purpose. See actual RMI registry connection port extraction below. + String urlString = "service:jmx:rmi:///jndi/rmi://" + JMX_SERVER_HOSTNAME + ":" + JMX_SERVER_PORT + "/jmxrmi"; + + JMXServiceURL url = new JMXServiceURL(urlString); + JMXConnector jmxConnector = null; + + try { + jmxConnector = JMXConnectorFactory.connect(url); + System.out.println("Successfully connected to: " + urlString); + } catch (Exception e) { + jmxConnector = null; + e.printStackTrace(); + Assert.fail(e.getMessage()); + } + + try { + MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection(); + String brokerName = "0.0.0.0"; // configured e.g. in broker.xml element + ObjectNameBuilder objectNameBuilder = ObjectNameBuilder.create(ActiveMQDefaultConfiguration.getDefaultJmxDomain(), brokerName, true); + ActiveMQServerControl activeMQServerControl = MBeanServerInvocationHandler.newProxyInstance(mBeanServerConnection, objectNameBuilder.getActiveMQServerObjectName(), ActiveMQServerControl.class, false); + + String addressName = "test_list_consumers_address"; + String queueName = "test_list_consumers_queue"; + activeMQServerControl.createAddress(addressName, RoutingType.ANYCAST.name()); + activeMQServerControl.createQueue(addressName, queueName, RoutingType.ANYCAST.name()); + String uri = "tcp://localhost:61616"; + try (ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory(uri, null)) { + cf.createConnection().createSession(true, Session.SESSION_TRANSACTED).createConsumer(new ActiveMQQueue(queueName)); + + String options = JsonUtil.toJsonObject(ImmutableMap.of("field","queue", "operation", "EQUALS", "value", queueName)).toString(); + String consumersAsJsonString = activeMQServerControl.listConsumers(options, 1, 10); + + JsonObject consumersAsJsonObject = JsonUtil.readJsonObject(consumersAsJsonString); + JsonArray array = (JsonArray) consumersAsJsonObject.get("data"); + + Assert.assertEquals("number of consumers returned from query", 1, array.size()); + JsonObject jsonConsumer = array.getJsonObject(0); + Assert.assertEquals("queue name in consumer", queueName, jsonConsumer.getString("queue")); + Assert.assertEquals("address name in consumer", addressName, jsonConsumer.getString("address")); + } + } finally { + jmxConnector.close(); + } + } +}