Repository: activemq-artemis Updated Branches: refs/heads/master 52470dcc8 -> 99b6a3425
NO-JIRA fix JMX examples Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/53bfb47b Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/53bfb47b Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/53bfb47b Branch: refs/heads/master Commit: 53bfb47b34b85212d0a13029df88a498ea908788 Parents: 52470dc Author: Justin Bertram <jbert...@apache.org> Authored: Thu Feb 22 15:19:01 2018 -0600 Committer: Michael Andre Pearce <michael.andre.pea...@me.com> Committed: Mon Feb 26 19:12:14 2018 +0000 ---------------------------------------------------------------------- examples/features/standard/jmx-ssl/pom.xml | 7 +- examples/features/standard/jmx-ssl/readme.md | 8 +- .../artemis/jms/example/JMXExample.java | 127 ----------------- .../artemis/jms/example/JMXOverSSLExample.java | 136 +++++++++++++++++++ .../artemis/jms/example/JMXExample.java | 8 +- examples/features/standard/pom.xml | 2 + 6 files changed, 151 insertions(+), 137 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/53bfb47b/examples/features/standard/jmx-ssl/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jmx-ssl/pom.xml b/examples/features/standard/jmx-ssl/pom.xml index b946d70..8e4a993 100644 --- a/examples/features/standard/jmx-ssl/pom.xml +++ b/examples/features/standard/jmx-ssl/pom.xml @@ -84,7 +84,10 @@ under the License. <goal>runClient</goal> </goals> <configuration> - <clientClass>org.apache.activemq.artemis.jms.example.JMXExample</clientClass> + <args> + <arg>${project.build.outputDirectory}/activemq/server0/</arg> + </args> + <clientClass>org.apache.activemq.artemis.jms.example.JMXOverSSLExample</clientClass> </configuration> </execution> <execution> @@ -103,7 +106,7 @@ under the License. <dependencies> <dependency> <groupId>org.apache.activemq.examples.broker</groupId> - <artifactId>jmx</artifactId> + <artifactId>jmx-ssl</artifactId> <version>${project.version}</version> </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/53bfb47b/examples/features/standard/jmx-ssl/readme.md ---------------------------------------------------------------------- diff --git a/examples/features/standard/jmx-ssl/readme.md b/examples/features/standard/jmx-ssl/readme.md index 2951564..e653012 100644 --- a/examples/features/standard/jmx-ssl/readme.md +++ b/examples/features/standard/jmx-ssl/readme.md @@ -1,12 +1,8 @@ # JMX Management Example -To run the example, simply type: - - mvn verify -Djavax.net.ssl.keyStore=target/server0/etc/activemq.example.keystore -Djavax.net.ssl.keyStorePassword=activemqexample -Djavax.net.ssl.trustStore=target/server0/etc/activemq.example.truststore -Djavax.net.ssl.trustStorePassword=activemqexample +To run the example, simply type **mvn verify** from this directory, or **mvn -PnoServer verify** if you want to start and create the broker manually. -from this directory, or add **-PnoServer** if you want to start and create the broker manually. - -This example shows how to manage ActiveMQ Artemis using [JMX using SSL](http://www.oracle.com/technetwork/java/javase/tech/javamanagement-140525.html) +This example shows how to manage ActiveMQ Artemis using [JMX over SSL](http://www.oracle.com/technetwork/java/javase/tech/javamanagement-140525.html) ## Example configuration http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/53bfb47b/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java b/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java deleted file mode 100644 index 9223a5e..0000000 --- a/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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.jms.example; - -import javax.jms.MessageConsumer; -import javax.jms.MessageProducer; -import javax.jms.Queue; -import javax.jms.QueueConnection; -import javax.jms.QueueConnectionFactory; -import javax.jms.QueueSession; -import javax.jms.Session; -import javax.jms.TextMessage; -import javax.management.MBeanServerConnection; -import javax.management.MBeanServerInvocationHandler; -import javax.management.ObjectName; -import javax.management.remote.JMXConnector; -import javax.management.remote.JMXConnectorFactory; -import javax.management.remote.JMXServiceURL; -import javax.naming.InitialContext; -import java.util.HashMap; - -import org.apache.activemq.artemis.api.core.RoutingType; -import org.apache.activemq.artemis.api.core.SimpleString; -import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder; -import org.apache.activemq.artemis.api.core.management.QueueControl; -import org.apache.activemq.artemis.jms.client.ActiveMQTextMessage; - -/** - * An example that shows how to manage ActiveMQ Artemis using JMX. - */ -public class JMXExample { - - private static final String JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"; - - public static void main(final String[] args) throws Exception { - QueueConnection connection = null; - InitialContext initialContext = null; - try { - // Step 1. Create an initial context to perform the JNDI lookup. - initialContext = new InitialContext(); - - // Step 2. Perfom a lookup on the queue - Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); - - // Step 3. Perform a lookup on the Connection Factory - QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory"); - - // Step 4.Create a JMS Connection - connection = cf.createQueueConnection(); - - // Step 5. Create a JMS Session - QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); - - // Step 6. Create a JMS Message Producer - MessageProducer producer = session.createProducer(queue); - - // Step 7. Create a Text Message - TextMessage message = session.createTextMessage("This is a text message"); - System.out.println("Sent message: " + message.getText()); - - // Step 8. Send the Message - producer.send(message); - - // Step 9. Retrieve the ObjectName of the queue. This is used to identify the server resources to manage - ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST); - - // Step 10. Create JMX Connector to connect to the server's MBeanServer - //we dont actually need credentials as the guest login i sused but this is how its done - HashMap env = new HashMap(); - String[] creds = {"guest", "guest"}; - env.put(JMXConnector.CREDENTIALS, creds); - - JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMXExample.JMX_URL), env); - - // Step 11. Retrieve the MBeanServerConnection - MBeanServerConnection mbsc = connector.getMBeanServerConnection(); - - // Step 12. Create a QueueControl proxy to manage the queue on the server - QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false); - // Step 13. Display the number of messages in the queue - System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages"); - - // Step 14. Remove the message sent at step #8 - System.out.println("message has been removed: " + queueControl.removeMessage(((ActiveMQTextMessage) message).getCoreMessage().getMessageID())); - - // Step 15. Display the number of messages in the queue - System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages"); - - // Step 16. We close the JMX connector - connector.close(); - - // Step 17. Create a JMS Message Consumer on the queue - MessageConsumer messageConsumer = session.createConsumer(queue); - - // Step 18. Start the Connection - connection.start(); - - // Step 19. Trying to receive a message. Since the only message in the queue was removed by a management - // operation, there is none to consume. - // The call will timeout after 5000ms and messageReceived will be null - TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000); - System.out.println("Received message: " + messageReceived); - } finally { - // Step 20. Be sure to close the resources! - if (initialContext != null) { - initialContext.close(); - } - if (connection != null) { - connection.close(); - } - } - } -} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/53bfb47b/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXOverSSLExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXOverSSLExample.java b/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXOverSSLExample.java new file mode 100644 index 0000000..b8b8ef7 --- /dev/null +++ b/examples/features/standard/jmx-ssl/src/main/java/org/apache/activemq/artemis/jms/example/JMXOverSSLExample.java @@ -0,0 +1,136 @@ +/* + * 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.jms.example; + +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.QueueConnection; +import javax.jms.QueueConnectionFactory; +import javax.jms.QueueSession; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.management.MBeanServerConnection; +import javax.management.MBeanServerInvocationHandler; +import javax.management.ObjectName; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; +import javax.naming.InitialContext; +import java.util.HashMap; + +import org.apache.activemq.artemis.api.core.RoutingType; +import org.apache.activemq.artemis.api.core.SimpleString; +import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder; +import org.apache.activemq.artemis.api.core.management.QueueControl; + +/** + * An example that shows how to manage ActiveMQ Artemis using JMX. + */ +public class JMXOverSSLExample { + + private static final String JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"; + + public static void main(final String[] args) throws Exception { + QueueConnection connection = null; + InitialContext initialContext = null; + try { + // Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. Perfom a lookup on the queue + Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); + + // Step 3. Perform a lookup on the Connection Factory + QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4.Create a JMS Connection + connection = cf.createQueueConnection(); + + // Step 5. Create a JMS Session + QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 6. Create a JMS Message Producer + MessageProducer producer = session.createProducer(queue); + + // Step 7. Create a Text Message + TextMessage message = session.createTextMessage("This is a text message"); + System.out.println("Sent message: " + message.getText()); + + // Step 8. Send the Message + producer.send(message); + + // Step 9. Retrieve the ObjectName of the queue. This is used to identify the server resources to manage + ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST); + + // Step 10. Create JMX Connector to connect to the server's MBeanServer + //we dont actually need credentials as the guest login i sused but this is how its done + HashMap env = new HashMap(); + String[] creds = {"guest", "guest"}; + env.put(JMXConnector.CREDENTIALS, creds); + + System.setProperty("javax.net.ssl.trustStore", args[0] + "activemq.example.truststore"); + System.setProperty("javax.net.ssl.trustStorePassword", "activemqexample"); + System.setProperty("javax.net.ssl.keyStore", args[0] + "activemq.example.keystore"); + System.setProperty("javax.net.ssl.keyStorePassword", "activemqexample"); + + JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMXOverSSLExample.JMX_URL), env); + + // Step 11. Retrieve the MBeanServerConnection + MBeanServerConnection mbsc = connector.getMBeanServerConnection(); + + // Step 12. Create a QueueControl proxy to manage the queue on the server + QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false); + // Step 13. Display the number of messages in the queue + System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages"); + + // Step 14. Remove the message sent at step #8 + System.out.println("message has been removed: " + queueControl.removeMessages(null)); + + // Step 15. Display the number of messages in the queue + System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages"); + + // Step 16. We close the JMX connector + connector.close(); + + // Step 17. Create a JMS Message Consumer on the queue + MessageConsumer messageConsumer = session.createConsumer(queue); + + // Step 18. Start the Connection + connection.start(); + + // Step 19. Trying to receive a message. Since the only message in the queue was removed by a management + // operation, there is none to consume. + // The call will timeout after 5000ms and messageReceived will be null + TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000); + + if (messageReceived != null) { + throw new IllegalStateException("message should be null!"); + } + + System.out.println("Received message: " + messageReceived); + } finally { + // Step 20. Be sure to close the resources! + if (initialContext != null) { + initialContext.close(); + } + if (connection != null) { + connection.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/53bfb47b/examples/features/standard/jmx/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jmx/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java b/examples/features/standard/jmx/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java index 9223a5e..d6548c7 100644 --- a/examples/features/standard/jmx/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java +++ b/examples/features/standard/jmx/src/main/java/org/apache/activemq/artemis/jms/example/JMXExample.java @@ -37,7 +37,6 @@ import org.apache.activemq.artemis.api.core.RoutingType; import org.apache.activemq.artemis.api.core.SimpleString; import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder; import org.apache.activemq.artemis.api.core.management.QueueControl; -import org.apache.activemq.artemis.jms.client.ActiveMQTextMessage; /** * An example that shows how to manage ActiveMQ Artemis using JMX. @@ -95,7 +94,7 @@ public class JMXExample { System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages"); // Step 14. Remove the message sent at step #8 - System.out.println("message has been removed: " + queueControl.removeMessage(((ActiveMQTextMessage) message).getCoreMessage().getMessageID())); + System.out.println("message has been removed: " + queueControl.removeMessages(null)); // Step 15. Display the number of messages in the queue System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages"); @@ -113,6 +112,11 @@ public class JMXExample { // operation, there is none to consume. // The call will timeout after 5000ms and messageReceived will be null TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000); + + if (messageReceived != null) { + throw new IllegalStateException("message should be null!"); + } + System.out.println("Received message: " + messageReceived); } finally { // Step 20. Be sure to close the resources! http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/53bfb47b/examples/features/standard/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/pom.xml b/examples/features/standard/pom.xml index b4d0f67..0d4b2f8 100644 --- a/examples/features/standard/pom.xml +++ b/examples/features/standard/pom.xml @@ -67,6 +67,7 @@ under the License. <module>jms-context</module> <module>jms-shared-consumer</module> <module>jmx</module> + <module>jmx-ssl</module> <module>large-message</module> <module>last-value-queue</module> <module>management</module> @@ -135,6 +136,7 @@ under the License. <module>jms-context</module> <module>jms-shared-consumer</module> <module>jmx</module> + <module>jmx-ssl</module> <module>large-message</module> <module>last-value-queue</module> <module>management</module>