This closes #170 on fixing examples

This commit is contained in:
Clebert Suconic 2015-03-11 19:15:07 -04:00
commit 670f5841ba
13 changed files with 131 additions and 64 deletions

View File

@ -21,6 +21,7 @@ import org.apache.activemq.core.server.ActiveMQMessageBundle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -37,6 +38,28 @@ public class SecurityConfiguration
*/ */
protected final Map<String, List<String>> roles = new HashMap<String, List<String>>(); protected final Map<String, List<String>> roles = new HashMap<String, List<String>>();
public SecurityConfiguration()
{
}
public SecurityConfiguration(Map<String, String> users, Map<String, List<String>> roles)
{
Iterator<Map.Entry<String, String>> iter = users.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry<String, String> entry = iter.next();
addUser(entry.getKey(), entry.getValue());
}
Iterator<Map.Entry<String, List<String>>> iter1 = roles.entrySet().iterator();
while (iter1.hasNext())
{
Map.Entry<String, List<String>> entry = iter1.next();
for (String role : entry.getValue())
{
addRole(entry.getKey(), role);
}
}
}
public void addUser(final String user, final String password) public void addUser(final String user, final String password)
{ {

View File

@ -34,38 +34,63 @@ under the License.
<ol> <ol>
<li>Create ActiveMQ core configuration files and make sure they are within your classpath. By default, ActiveMQ <li>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".</li> expects the configuration file name to be "activemq-configuration.xml".</li>
<li>Create and start ActiveMQ JMS server</li> <li>Create an embedded ActiveMQ JMS server</li>
<pre class="prettyprint"> <pre class="prettyprint">
<code>EmbeddedJMS jmsServer = new EmbeddedJMS(); <code>EmbeddedJMS jmsServer = new EmbeddedJMS();</code>
jmsServer.start();</code>
</pre> </pre>
<li>Setup security configurations</li>
<pre class="prettyprint">
<code>SecurityConfiguration securityConfig = new SecurityConfiguration();
securityConfig.addUser("guest", "guest");
securityConfig.addRole("guest", "guest");
securityConfig.setDefaultUser("guest");
jmsServer.setSecurityManager(new ActiveMQSecurityManagerImpl(securityConfig));</code>
</pre>
<li>Start the embedded ActiveMQ JMS server</li>
<pre class="prettyprint">
<code>jmsServer.start()</code>
</pre>
<li>Create JMS resources (connection factory and queue) for the example</li>
<pre class="prettyprint">
<code>JMSServerManager jmsServerManager = jmsServer.getJMSServerManager();
List<String> connectors = new ArrayList<String>();
connectors.add("in-vm");
jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory");
jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue");</code>
</pre>
<p>At this point the JMS server is started and any JMS clients can look up JMS resources from the JNDI to send/receive <p>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 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.</p> used to run the JMS server.</p>
<li>Lookup JMS resources defined in the configuration </li> <li>Lookup JMS resources defined in the configuration </li>
<pre class="prettyprint"> <pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf"); <code>ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
Queue queue = (Queue)context.lookup("/queue/queue1");</code> Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");</code>
</pre> </pre>
<li>Send and receive a message using JMS API</li> <li>Send and receive a message using JMS API</li>
<p>See the <a href="../../queue/readme.html">Queue Example</a> for detailed steps to send and receive a JMS message</p> <p>See the <a href="../queue/readme.html">Queue Example</a> for detailed steps to send and receive a JMS message</p>
<p>Finally, we stop the JMS server and its associated resources.</p> <p>Finally, we stop the JMS server and its associated resources.</p>
<li>Close the connection</li>
<pre class="prettyprint">
<code>if (connection != null)
{
connection.close();
}</code>
</pre>
<li>Stop the JMS server</li> <li>Stop the JMS server</li>
<pre class="prettyprint"> <pre class="prettyprint">
<code>jmsServer.stop();</code> <code>jmsServer.stop();</code>
</pre> </pre>
<li>Stop the JNDI server</li>
<pre class="prettyprint">
<code>naming.stop();
jndiServer.stop();</code>
</pre>
</ol> </ol>
</body> </body>
</html> </html>

View File

@ -29,8 +29,10 @@ import javax.jms.Session;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import org.apache.activemq.common.example.ActiveMQExample; 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.embedded.EmbeddedJMS;
import org.apache.activemq.jms.server.JMSServerManager; import org.apache.activemq.jms.server.JMSServerManager;
import org.apache.activemq.spi.core.security.ActiveMQSecurityManagerImpl;
import org.apache.activemq.api.jms.JMSFactoryType; import org.apache.activemq.api.jms.JMSFactoryType;
/** /**
@ -50,6 +52,13 @@ public class EmbeddedExample extends ActiveMQExample
try try
{ {
EmbeddedJMS jmsServer = new EmbeddedJMS(); 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(); jmsServer.start();
System.out.println("Started Embedded JMS Server"); System.out.println("Started Embedded JMS Server");
@ -57,6 +66,8 @@ public class EmbeddedExample extends ActiveMQExample
List<String> connectors = new ArrayList<String>(); List<String> connectors = new ArrayList<String>();
connectors.add("in-vm"); connectors.add("in-vm");
jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory"); jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory");
jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory"); ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue"); Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");

View File

@ -22,15 +22,14 @@ under the License.
xmlns="urn:activemq" xmlns="urn:activemq"
xsi:schemaLocation="urn:activemq /schema/activemq-server.xsd"> xsi:schemaLocation="urn:activemq /schema/activemq-server.xsd">
<jms xmlns="urn:activemq:jms">
<!--the queue used by the example-->
<queue name="exampleQueue"/>
</jms>
<core xmlns="urn:activemq:core"> <core xmlns="urn:activemq:core">
<persistence-enabled>false</persistence-enabled> <persistence-enabled>false</persistence-enabled>
<connectors>
<connector name="in-vm">vm://0</connector>
</connectors>
<acceptors> <acceptors>
<acceptor name="in-vm">vm://0</acceptor> <acceptor name="in-vm">vm://0</acceptor>
</acceptors> </acceptors>

View File

@ -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

View File

@ -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

View File

@ -35,7 +35,7 @@ under the License.
<listener> <listener>
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class> <listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
</listener> </listener>
<listener> <listener>

View File

@ -29,7 +29,7 @@ under the License.
<listener> <listener>
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class> <listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
</listener> </listener>
<listener> <listener>

View File

@ -29,7 +29,7 @@ under the License.
<listener> <listener>
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class> <listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
</listener> </listener>
<listener> <listener>

View File

@ -29,7 +29,7 @@ under the License.
<listener> <listener>
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class> <listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
</listener> </listener>
<listener> <listener>

View File

@ -90,12 +90,16 @@ under the License.
</goals> </goals>
<configuration> <configuration>
<clientClass>org.apache.activemq.jms.example.ScaleDownExample</clientClass> <clientClass>org.apache.activemq.jms.example.ScaleDownExample</clientClass>
<systemProperties> <args>
<property> <param>tcp://localhost:61616</param>
<name>exampleConfigDir</name> <param>tcp://localhost:61617</param>
<value>${basedir}/target/classes/activemq</value> </args>
</property> <systemProperties>
</systemProperties> <property>
<name>exampleConfigDir</name>
<value>${basedir}/target/classes/activemq</value>
</property>
</systemProperties>
</configuration> </configuration>
</execution> </execution>
<execution> <execution>

View File

@ -19,6 +19,7 @@ package org.apache.activemq.jms.example;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.jms.Destination; import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.TextMessage; import javax.jms.TextMessage;
@ -50,9 +51,10 @@ public class MessageSender
public void send(String msg) public void send(String msg)
{ {
Connection conn = null;
try try
{ {
Connection conn = connectionFactory.createConnection(); conn = connectionFactory.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination); MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage(msg); TextMessage message = session.createTextMessage(msg);
@ -62,5 +64,19 @@ public class MessageSender
{ {
ex.printStackTrace(); ex.printStackTrace();
} }
finally
{
if (conn != null)
{
try
{
conn.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
} }
} }

View File

@ -24,8 +24,31 @@ under the License.
xsi:schemaLocation="http://www.springframework.org/schema/beans xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="securityManager" class="org.apache.activemq.spi.core.security.ActiveMQSecurityManagerImpl">
<constructor-arg>
<bean class="org.apache.activemq.core.config.impl.SecurityConfiguration">
<constructor-arg name="users">
<map>
<entry key="guest" value="guest"/>
</map>
</constructor-arg>
<constructor-arg name="roles">
<map>
<entry key="guest">
<list>
<value>guest</value>
</list>
</entry>
</map>
</constructor-arg>
<property name="DefaultUser" value="guest"/>
</bean>
</constructor-arg>
</bean>
<bean id="EmbeddedJms" class="org.apache.activemq.integration.spring.SpringJmsBootstrap" init-method="start" <bean id="EmbeddedJms" class="org.apache.activemq.integration.spring.SpringJmsBootstrap" init-method="start"
destroy-method="stop"> destroy-method="stop">
<property name="SecurityManager" ref="securityManager"/>
</bean> </bean>
<bean id="connectionFactory" class="org.apache.activemq.jms.client.ActiveMQJMSConnectionFactory"> <bean id="connectionFactory" class="org.apache.activemq.jms.client.ActiveMQJMSConnectionFactory">