From 4b7eafddc1cbaf87eb7eb06919bd1d928042e339 Mon Sep 17 00:00:00 2001 From: Howard Gao Date: Tue, 10 Mar 2015 21:28:07 +0800 Subject: [PATCH 1/4] Fix jms/embedded-simple example --- examples/jms/embedded-simple/readme.html | 53 ++++++++++++++----- .../activemq/jms/example/EmbeddedExample.java | 11 ++++ .../main/resources/activemq-configuration.xml | 9 ++-- .../main/resources/activemq-roles.properties | 17 ------ .../main/resources/activemq-users.properties | 17 ------ 5 files changed, 54 insertions(+), 53 deletions(-) delete mode 100644 examples/jms/embedded-simple/src/main/resources/activemq-roles.properties delete mode 100644 examples/jms/embedded-simple/src/main/resources/activemq-users.properties diff --git a/examples/jms/embedded-simple/readme.html b/examples/jms/embedded-simple/readme.html index 7a4104c20f..bf412588d6 100644 --- a/examples/jms/embedded-simple/readme.html +++ b/examples/jms/embedded-simple/readme.html @@ -34,38 +34,63 @@ under the License.
  1. Create ActiveMQ core configuration files and make sure they are within your classpath. By default, ActiveMQ - expects the classnames to be "activemq-configuration.xml", "activemq-jms.xml", and "activemq-users.xml".
  2. -
  3. Create and start ActiveMQ JMS server
  4. + expects the configuration file name to be "activemq-configuration.xml". +
  5. Create an embedded ActiveMQ JMS server
  6. -            EmbeddedJMS jmsServer = new EmbeddedJMS();
    -            jmsServer.start();
    +            EmbeddedJMS jmsServer = new EmbeddedJMS();
              
    - + +
  7. Setup security configurations
  8. +
    +            SecurityConfiguration securityConfig = new SecurityConfiguration();
    +            securityConfig.addUser("guest", "guest");
    +            securityConfig.addRole("guest", "guest");
    +            securityConfig.setDefaultUser("guest");
    +            jmsServer.setSecurityManager(new ActiveMQSecurityManagerImpl(securityConfig));
    +         
    + +
  9. Start the embedded ActiveMQ JMS server
  10. +
    +            jmsServer.start()
    +         
    + +
  11. Create JMS resources (connection factory and queue) for the example
  12. +
    +            JMSServerManager jmsServerManager = jmsServer.getJMSServerManager();
    +            List connectors = new ArrayList();
    +            connectors.add("in-vm");
    +            jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory");
    +            jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue");
    +         
    +

    At this point the JMS server is started and any JMS clients can look up JMS resources from the JNDI to send/receive messages from the server. To keep the example simple, we will send and receive a JMS message from the same JVM used to run the JMS server.

  13. Lookup JMS resources defined in the configuration
  14. -            ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
    -            Queue queue = (Queue)context.lookup("/queue/queue1");
    +            ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
    +            Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");
              
  15. Send and receive a message using JMS API
  16. -

    See the Queue Example for detailed steps to send and receive a JMS message

    +

    See the Queue Example for detailed steps to send and receive a JMS message

    Finally, we stop the JMS server and its associated resources.

    - + +
  17. Close the connection
  18. +
    +            if (connection != null)
    +            {
    +               connection.close();
    +            }
    +         
    +
  19. Stop the JMS server
  20.              jmsServer.stop();
              
    -
  21. Stop the JNDI server
  22. -
    -            naming.stop();
    -            jndiServer.stop();
    -         
diff --git a/examples/jms/embedded-simple/src/main/java/org/apache/activemq/jms/example/EmbeddedExample.java b/examples/jms/embedded-simple/src/main/java/org/apache/activemq/jms/example/EmbeddedExample.java index 008a6046db..838c3fd6bb 100644 --- a/examples/jms/embedded-simple/src/main/java/org/apache/activemq/jms/example/EmbeddedExample.java +++ b/examples/jms/embedded-simple/src/main/java/org/apache/activemq/jms/example/EmbeddedExample.java @@ -29,8 +29,10 @@ import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.common.example.ActiveMQExample; +import org.apache.activemq.core.config.impl.SecurityConfiguration; import org.apache.activemq.jms.server.embedded.EmbeddedJMS; import org.apache.activemq.jms.server.JMSServerManager; +import org.apache.activemq.spi.core.security.ActiveMQSecurityManagerImpl; import org.apache.activemq.api.jms.JMSFactoryType; /** @@ -50,6 +52,13 @@ public class EmbeddedExample extends ActiveMQExample try { EmbeddedJMS jmsServer = new EmbeddedJMS(); + + SecurityConfiguration securityConfig = new SecurityConfiguration(); + securityConfig.addUser("guest", "guest"); + securityConfig.addRole("guest", "guest"); + securityConfig.setDefaultUser("guest"); + jmsServer.setSecurityManager(new ActiveMQSecurityManagerImpl(securityConfig)); + jmsServer.start(); System.out.println("Started Embedded JMS Server"); @@ -57,6 +66,8 @@ public class EmbeddedExample extends ActiveMQExample List connectors = new ArrayList(); connectors.add("in-vm"); jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory"); + jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue"); + ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory"); Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue"); diff --git a/examples/jms/embedded-simple/src/main/resources/activemq-configuration.xml b/examples/jms/embedded-simple/src/main/resources/activemq-configuration.xml index fdc8811736..68cca46ace 100644 --- a/examples/jms/embedded-simple/src/main/resources/activemq-configuration.xml +++ b/examples/jms/embedded-simple/src/main/resources/activemq-configuration.xml @@ -22,15 +22,14 @@ under the License. xmlns="urn:activemq" xsi:schemaLocation="urn:activemq /schema/activemq-server.xsd"> - - - - - false + + vm://0 + + vm://0 diff --git a/examples/jms/embedded-simple/src/main/resources/activemq-roles.properties b/examples/jms/embedded-simple/src/main/resources/activemq-roles.properties deleted file mode 100644 index 4e2d44cec4..0000000000 --- a/examples/jms/embedded-simple/src/main/resources/activemq-roles.properties +++ /dev/null @@ -1,17 +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. -## --------------------------------------------------------------------------- -guest=guest \ No newline at end of file diff --git a/examples/jms/embedded-simple/src/main/resources/activemq-users.properties b/examples/jms/embedded-simple/src/main/resources/activemq-users.properties deleted file mode 100644 index 4e2d44cec4..0000000000 --- a/examples/jms/embedded-simple/src/main/resources/activemq-users.properties +++ /dev/null @@ -1,17 +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. -## --------------------------------------------------------------------------- -guest=guest \ No newline at end of file From 9aaaf0a52354272001525fbc7630ac91653f0e99 Mon Sep 17 00:00:00 2001 From: Howard Gao Date: Wed, 11 Mar 2015 10:54:10 +0800 Subject: [PATCH 2/4] Fix jms/rest example configuration --- examples/jms/rest/dup-send/src/main/webapp/WEB-INF/web.xml | 2 +- .../jms/rest/javascript-chat/src/main/webapp/WEB-INF/web.xml | 2 +- examples/jms/rest/jms-to-rest/src/main/webapp/WEB-INF/web.xml | 2 +- examples/jms/rest/push/src/main/webapp/WEB-INF/web.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/jms/rest/dup-send/src/main/webapp/WEB-INF/web.xml b/examples/jms/rest/dup-send/src/main/webapp/WEB-INF/web.xml index 1266949ae4..cc75fc06ed 100644 --- a/examples/jms/rest/dup-send/src/main/webapp/WEB-INF/web.xml +++ b/examples/jms/rest/dup-send/src/main/webapp/WEB-INF/web.xml @@ -35,7 +35,7 @@ under the License. - org.apache.activemq.rest.integration.HornetqBootstrapListener + org.apache.activemq.rest.integration.ActiveMQBootstrapListener diff --git a/examples/jms/rest/javascript-chat/src/main/webapp/WEB-INF/web.xml b/examples/jms/rest/javascript-chat/src/main/webapp/WEB-INF/web.xml index f4284d92a6..d3ceb423b3 100644 --- a/examples/jms/rest/javascript-chat/src/main/webapp/WEB-INF/web.xml +++ b/examples/jms/rest/javascript-chat/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ under the License. - org.apache.activemq.rest.integration.HornetqBootstrapListener + org.apache.activemq.rest.integration.ActiveMQBootstrapListener diff --git a/examples/jms/rest/jms-to-rest/src/main/webapp/WEB-INF/web.xml b/examples/jms/rest/jms-to-rest/src/main/webapp/WEB-INF/web.xml index f4284d92a6..d3ceb423b3 100644 --- a/examples/jms/rest/jms-to-rest/src/main/webapp/WEB-INF/web.xml +++ b/examples/jms/rest/jms-to-rest/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ under the License. - org.apache.activemq.rest.integration.HornetqBootstrapListener + org.apache.activemq.rest.integration.ActiveMQBootstrapListener diff --git a/examples/jms/rest/push/src/main/webapp/WEB-INF/web.xml b/examples/jms/rest/push/src/main/webapp/WEB-INF/web.xml index 8c96e2586e..e851072855 100644 --- a/examples/jms/rest/push/src/main/webapp/WEB-INF/web.xml +++ b/examples/jms/rest/push/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ under the License. - org.apache.activemq.rest.integration.HornetqBootstrapListener + org.apache.activemq.rest.integration.ActiveMQBootstrapListener From 5fe83ce8d11ac8681d1cf6385bbb0d98266586ed Mon Sep 17 00:00:00 2001 From: Howard Gao Date: Wed, 11 Mar 2015 11:26:04 +0800 Subject: [PATCH 3/4] Fix jms/scale-down example pom.xml --- examples/jms/scale-down/pom.xml | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/jms/scale-down/pom.xml b/examples/jms/scale-down/pom.xml index 866371e871..1419f880c0 100644 --- a/examples/jms/scale-down/pom.xml +++ b/examples/jms/scale-down/pom.xml @@ -90,12 +90,16 @@ under the License. org.apache.activemq.jms.example.ScaleDownExample - - - exampleConfigDir - ${basedir}/target/classes/activemq - - + + tcp://localhost:61616 + tcp://localhost:61617 + + + + exampleConfigDir + ${basedir}/target/classes/activemq + + From d7cd6455953fc4985fce387ea6b9ca6a4510ca6b Mon Sep 17 00:00:00 2001 From: Howard Gao Date: Wed, 11 Mar 2015 16:48:44 +0800 Subject: [PATCH 4/4] Fix jms/spring-integration example --- .../config/impl/SecurityConfiguration.java | 23 +++++++++++++++++++ .../activemq/jms/example/MessageSender.java | 18 ++++++++++++++- .../src/main/resources/spring-jms-beans.xml | 23 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/activemq-server/src/main/java/org/apache/activemq/core/config/impl/SecurityConfiguration.java b/activemq-server/src/main/java/org/apache/activemq/core/config/impl/SecurityConfiguration.java index a40f3615d3..45566e1ed9 100644 --- a/activemq-server/src/main/java/org/apache/activemq/core/config/impl/SecurityConfiguration.java +++ b/activemq-server/src/main/java/org/apache/activemq/core/config/impl/SecurityConfiguration.java @@ -21,6 +21,7 @@ import org.apache.activemq.core.server.ActiveMQMessageBundle; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -37,6 +38,28 @@ public class SecurityConfiguration */ protected final Map> roles = new HashMap>(); + public SecurityConfiguration() + { + } + + public SecurityConfiguration(Map users, Map> roles) + { + Iterator> iter = users.entrySet().iterator(); + while (iter.hasNext()) + { + Map.Entry entry = iter.next(); + addUser(entry.getKey(), entry.getValue()); + } + Iterator>> iter1 = roles.entrySet().iterator(); + while (iter1.hasNext()) + { + Map.Entry> entry = iter1.next(); + for (String role : entry.getValue()) + { + addRole(entry.getKey(), role); + } + } + } public void addUser(final String user, final String password) { diff --git a/examples/jms/spring-integration/src/main/java/org/apache/activemq/jms/example/MessageSender.java b/examples/jms/spring-integration/src/main/java/org/apache/activemq/jms/example/MessageSender.java index 2b08ccc208..1d5401db68 100644 --- a/examples/jms/spring-integration/src/main/java/org/apache/activemq/jms/example/MessageSender.java +++ b/examples/jms/spring-integration/src/main/java/org/apache/activemq/jms/example/MessageSender.java @@ -19,6 +19,7 @@ package org.apache.activemq.jms.example; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; +import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; @@ -50,9 +51,10 @@ public class MessageSender public void send(String msg) { + Connection conn = null; try { - Connection conn = connectionFactory.createConnection(); + conn = connectionFactory.createConnection(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(msg); @@ -62,5 +64,19 @@ public class MessageSender { ex.printStackTrace(); } + finally + { + if (conn != null) + { + try + { + conn.close(); + } + catch (JMSException e) + { + e.printStackTrace(); + } + } + } } } diff --git a/examples/jms/spring-integration/src/main/resources/spring-jms-beans.xml b/examples/jms/spring-integration/src/main/resources/spring-jms-beans.xml index bcbb01ba0a..14bb5c8fc3 100644 --- a/examples/jms/spring-integration/src/main/resources/spring-jms-beans.xml +++ b/examples/jms/spring-integration/src/main/resources/spring-jms-beans.xml @@ -24,8 +24,31 @@ under the License. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> + + + + + + + + + + + + + guest + + + + + + + + + +