2015-01-05 07:45:36 -05:00
|
|
|
<!--
|
|
|
|
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.
|
|
|
|
-->
|
|
|
|
|
2014-10-31 06:20:28 -04:00
|
|
|
<html>
|
|
|
|
<head>
|
2014-11-19 03:44:57 -05:00
|
|
|
<title>ActiveMQ Embedded JMS Server Example</title>
|
2014-10-31 06:20:28 -04:00
|
|
|
<link rel="stylesheet" type="text/css" href="../common/common.css" />
|
|
|
|
<link rel="stylesheet" type="text/css" href="../common/prettify.css" />
|
|
|
|
<script type="text/javascript" src="../common/prettify.js"></script>
|
|
|
|
</head>
|
|
|
|
<body onload="prettyPrint()">
|
|
|
|
<h1>Embedded JMS Server Example</h1>
|
|
|
|
|
2014-11-19 03:44:57 -05:00
|
|
|
<p>This examples shows how to setup and run an embedded JMS server using ActiveMQ along with ActiveMQ configuration files.</p>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
<h2>Example step-by-step</h2>
|
2015-03-07 11:51:01 -05:00
|
|
|
<p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
<ol>
|
2014-11-19 03:44:57 -05:00
|
|
|
<li>Create ActiveMQ core configuration files and make sure they are within your classpath. By default, ActiveMQ
|
2015-03-10 09:28:07 -04:00
|
|
|
expects the configuration file name to be "activemq-configuration.xml".</li>
|
|
|
|
<li>Create an embedded ActiveMQ JMS server</li>
|
2014-10-31 06:20:28 -04:00
|
|
|
<pre class="prettyprint">
|
2015-03-10 09:28:07 -04:00
|
|
|
<code>EmbeddedJMS jmsServer = new EmbeddedJMS();</code>
|
2014-10-31 06:20:28 -04:00
|
|
|
</pre>
|
2015-03-10 09:28:07 -04:00
|
|
|
|
|
|
|
<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>
|
|
|
|
|
2014-10-31 06:20:28 -04:00
|
|
|
<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
|
|
|
|
used to run the JMS server.</p>
|
|
|
|
|
|
|
|
<li>Lookup JMS resources defined in the configuration </li>
|
|
|
|
<pre class="prettyprint">
|
2015-03-10 09:28:07 -04:00
|
|
|
<code>ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
|
|
|
|
Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");</code>
|
2014-10-31 06:20:28 -04:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
<li>Send and receive a message using JMS API</li>
|
2015-03-10 09:28:07 -04:00
|
|
|
<p>See the <a href="../queue/readme.html">Queue Example</a> for detailed steps to send and receive a JMS message</p>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
<p>Finally, we stop the JMS server and its associated resources.</p>
|
2015-03-10 09:28:07 -04:00
|
|
|
|
|
|
|
<li>Close the connection</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>if (connection != null)
|
|
|
|
{
|
|
|
|
connection.close();
|
|
|
|
}</code>
|
|
|
|
</pre>
|
|
|
|
|
2014-10-31 06:20:28 -04:00
|
|
|
<li>Stop the JMS server</li>
|
|
|
|
<pre class="prettyprint">
|
|
|
|
<code>jmsServer.stop();</code>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
</ol>
|
|
|
|
</body>
|
2015-01-05 07:45:36 -05:00
|
|
|
</html>
|