activemq-artemis/examples/jms/stomp1.1
Clebert Suconic 070f641f0d Fixing examples commons
Examples/common was wrongly renamed long time ago, commons/prettify was broken and this will fix formatting on the README files
2015-05-07 09:08:20 -04:00
..
src/main fixed examples and some docs after Artemis renaming 2015-04-30 10:44:16 +01:00
pom.xml Fixing examples commons 2015-05-07 09:08:20 -04:00
readme.html added examples to release profile 2015-03-08 13:34:02 +00: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 Stomp 1.1 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>Stomp 1.1 Example</h1>

     <p>This example shows you how to configure ActiveMQ to send and receive Stomp messages using Stomp 1.1 protocol.</p>
     <p>The example will start a ActiveMQ server configured with Stomp and JMS.</p>
     <p>The client will open a socket to initiate a Stomp 1.1 connection and then send one Stomp message (using TCP directly). 
       The client will then consume a message from a JMS Queue and check it is the message sent with Stomp.</p>

     <h2>Example step-by-step</h2>
     <p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>

     <ol>
        <li>We create a TCP socket to connect to the Stomp port
        <pre class="prettyprint">
          Socket socket = new Socket("localhost", 61613); 
        </pre>

        <li>We negotiate a Stomp 1.1 connection to the server</li>
        <pre class="prettyprint">
         String connectFrame = "CONNECT\n" + 
            "accept-version:1.1\n" +
            "host:localhost\n" +
            "login:guest\n" +
            "passcode:guest\n" +
            "request-id:1\n" +
            "\n" +
            END_OF_FRAME;
          sendFrame(socket, connectFrame);
        </pre>
        
        <li>We receive a response showing that the connection version</li>
        <pre>
         String response = receiveFrame(socket);
         System.out.println("response: " + response);
        </pre>

        <li>We send a SEND frame (a Stomp message) to the destination <code>jms.queue.exampleQueue</code> 
          (which corresponds to the ActiveMQ address for the JMS Queue <code>exampleQueue</code>) with a text body</li>
        <pre class="prettyprint">
          String text = "Hello World from Stomp 1.1 !";
          String message = "SEND\n" + 
             "destination:jms.queue.exampleQueue\n" +
             "\n" +
             text +
             END_OF_FRAME;
          sendFrame(socket, message);
          System.out.println("Sent Stomp message: " + text);
        </pre>

        <li>We send a DISCONNECT frame to disconnect from the server</li>
        <pre class="prettyprint">
          String disconnectFrame = "DISCONNECT\n" +
             "\n" +
             Stomp.NULL;
          sendFrame(socket, disconnectFrame);
        </pre>

        <li>We close the TCP socket</li>
        <pre class="prettyprint">
          socket.close();
        </pre>

        <li>We create an initial context to perform the JNDI lookup.</li>
        <pre class="prettyprint">
          initialContext = getContext(0);
       </pre>

        <li>We perform a lookup on the queue and the connection factory</li>
        <pre class="prettyprint">
          Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
          ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
        </pre>

        <li>We create a JMS Connection, Session and a MessageConsumer on the queue</li>
        <pre class="prettyprint">
          connection = cf.createConnection();
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          MessageConsumer consumer = session.createConsumer(queue);
        </pre>

        <li>We start the connection</li>
        <pre class="prettyprint">
           <code>connection.start();</code>
        </pre>

        <li>We receive the message. Stomp messages are mapped to JMS TextMessage.</li>
        <pre class="prettyprint">
          TextMessage messageReceived = (TextMessage)consumer.receive(5000);
          System.out.println("Received JMS message: " + messageReceived.getText());
        </pre>

        <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>

        <pre class="prettyprint">
           <code>finally
           {
              if (initialContext != null)
              {
                initialContext.close();
              }
              if (connection != null)
              {
                 connection.close();
              }
           }</code>
        </pre>



     </ol>
  </body>
</html>