Fix jms/embedded-simple example

This commit is contained in:
Howard Gao 2015-03-10 21:28:07 +08:00
parent dba926a1c6
commit 4b7eafddc1
5 changed files with 54 additions and 53 deletions

View File

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

View File

@ -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");

View File

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

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