This closes #170 on fixing examples
This commit is contained in:
commit
670f5841ba
|
@ -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<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)
|
||||
{
|
||||
|
|
|
@ -34,11 +34,33 @@ under the License.
|
|||
|
||||
<ol>
|
||||
<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>
|
||||
<li>Create and start ActiveMQ JMS server</li>
|
||||
expects the configuration file name to be "activemq-configuration.xml".</li>
|
||||
<li>Create an embedded ActiveMQ JMS server</li>
|
||||
<pre class="prettyprint">
|
||||
<code>EmbeddedJMS jmsServer = new EmbeddedJMS();
|
||||
jmsServer.start();</code>
|
||||
<code>EmbeddedJMS jmsServer = new EmbeddedJMS();</code>
|
||||
</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
|
||||
|
@ -47,25 +69,28 @@ under the License.
|
|||
|
||||
<li>Lookup JMS resources defined in the configuration </li>
|
||||
<pre class="prettyprint">
|
||||
<code>ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
|
||||
Queue queue = (Queue)context.lookup("/queue/queue1");</code>
|
||||
<code>ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
|
||||
Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");</code>
|
||||
</pre>
|
||||
|
||||
<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>
|
||||
|
||||
<li>Close the connection</li>
|
||||
<pre class="prettyprint">
|
||||
<code>if (connection != null)
|
||||
{
|
||||
connection.close();
|
||||
}</code>
|
||||
</pre>
|
||||
|
||||
<li>Stop the JMS server</li>
|
||||
<pre class="prettyprint">
|
||||
<code>jmsServer.stop();</code>
|
||||
</pre>
|
||||
|
||||
<li>Stop the JNDI server</li>
|
||||
<pre class="prettyprint">
|
||||
<code>naming.stop();
|
||||
jndiServer.stop();</code>
|
||||
</pre>
|
||||
</ol>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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<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");
|
||||
|
||||
ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
|
||||
Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");
|
||||
|
||||
|
|
|
@ -22,15 +22,14 @@ under the License.
|
|||
xmlns="urn:activemq"
|
||||
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">
|
||||
|
||||
<persistence-enabled>false</persistence-enabled>
|
||||
|
||||
<connectors>
|
||||
<connector name="in-vm">vm://0</connector>
|
||||
</connectors>
|
||||
|
||||
<acceptors>
|
||||
<acceptor name="in-vm">vm://0</acceptor>
|
||||
</acceptors>
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -35,7 +35,7 @@ under the License.
|
|||
|
||||
|
||||
<listener>
|
||||
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class>
|
||||
<listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
|
|
|
@ -29,7 +29,7 @@ under the License.
|
|||
|
||||
|
||||
<listener>
|
||||
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class>
|
||||
<listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
|
|
|
@ -29,7 +29,7 @@ under the License.
|
|||
|
||||
|
||||
<listener>
|
||||
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class>
|
||||
<listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
|
|
|
@ -29,7 +29,7 @@ under the License.
|
|||
|
||||
|
||||
<listener>
|
||||
<listener-class>org.apache.activemq.rest.integration.HornetqBootstrapListener</listener-class>
|
||||
<listener-class>org.apache.activemq.rest.integration.ActiveMQBootstrapListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
|
|
|
@ -90,6 +90,10 @@ under the License.
|
|||
</goals>
|
||||
<configuration>
|
||||
<clientClass>org.apache.activemq.jms.example.ScaleDownExample</clientClass>
|
||||
<args>
|
||||
<param>tcp://localhost:61616</param>
|
||||
<param>tcp://localhost:61617</param>
|
||||
</args>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>exampleConfigDir</name>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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">
|
||||
|
||||
<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"
|
||||
destroy-method="stop">
|
||||
<property name="SecurityManager" ref="securityManager"/>
|
||||
</bean>
|
||||
|
||||
<bean id="connectionFactory" class="org.apache.activemq.jms.client.ActiveMQJMSConnectionFactory">
|
||||
|
|
Loading…
Reference in New Issue