activemq-artemis/examples/core/embedded
Martyn Taylor 2b4e7babb6 Prepare examples,tests for release 2015-03-06 15:28:08 +00:00
..
src/main/java/org/apache/activemq/core/example ACTIVEMQ6-43 Replace License Headers on codebase 2014-12-01 10:20:24 +08:00
pom.xml Prepare examples,tests for release 2015-03-06 15:28:08 +00:00
readme.html ACTIVEMQ6-43(reopened) : Replace License Headers on codebase 2015-01-05 13:14:25 -05:00

readme.html

<!--
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.
-->

<html>
  <head>
    <title>ActiveMQ Embedded Example</title>
    <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 Example</h1>

     <p>This example shows how to setup and run ActiveMQ embedded.</p>
     <p>ActiveMQ was designed to use POJOs (Plain Old Java Objects), what makes embedding ActiveMQ as simple as instantiating a few objects.</p>
     <p>In this example, we are using two jars:</p>
     <ul>
        <li>activemq-server.jar</li>
        <li>netty.jar</li>
     </ul>
     
     <p>ActiveMQ Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.</p>

     <h2>Example step-by-step</h2>     
     <p><i>To run the example, simply type <code>mvn -Pexample</code> from this directory</i></p>
     <p>In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, ActiveMQServer, start it and operate on JMS regularly</p>

     <ol>
        <li>Create the Configuration, and set the properties accordingly</li>
        <pre class="prettyprint">
           Configuration configuration = new ConfigurationImpl();
           configuration.setEnablePersistence(false);
           configuration.setSecurityEnabled(false);
           configuration.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
        </pre>
         
        <li>Create and start the server</li>
        <pre class="prettyprint">
           ActiveMQServer server = ActiveMQ.newActiveMQServer(configuration);
           server.start();
        </pre>

        <li>As we are not using a JNDI environment we instantiate the objects directly</li>
        <pre class="prettyprint">
           ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(InVMConnectorFactory.class.getName()));
           ClientSessionFactory sf = serverLocator.createSessionFactory();
        </pre>

        <li>Create a Core Queue</li>
        <pre class="prettyprint">
           ClientSession coreSession = sf.createSession(false, false, false);
           final String queueName = "queue.exampleQueue";
           coreSession.createQueue(queueName, queueName, true);
           coreSession.close();
        </pre>

        <li>Create the session and producer</li>
        <pre class="prettyprint">
            session = sf.createSession();
                                   
            ClientProducer producer = session.createProducer(queueName);
        </pre>

        <li>Create and send a Message</li>
        <pre class="prettyprint">
           ClientMessage message = session.createMessage(false);
           message.putStringProperty(propName, "Hello sent at " + new Date());
           System.out.println("Sending the message.");
           producer.send(message);
        </pre>

        <li>Create the message consumer and start the connection</li>
        <pre class="prettyprint">
           ClientConsumer messageConsumer = session.createConsumer(queueName);
           session.start();
        </pre>

        <li>Receive the message</li>
        <pre class="prettyprint">
           ClientMessage messageReceived = messageConsumer.receive(1000);
           System.out.println("Received TextMessage:" + messageReceived.getProperty(propName));
        </pre>

        <li>Be sure to close our resources!</li>

        <pre class="prettyprint">
           if (sf != null)
           {
              sf.close();
           }
        </pre>

        <li>Stop the server</li>

        <pre class="prettyprint">
           server.stop();
        </pre>
     </ol>
  </body>
</html>