activemq-artemis/examples/jms/stomp1.2
Thiago Kronig d48b4f4770 ARTEMIS-129 License header should be a normal comment
To reproduce this commit, apply a replace regex rule using:

    search regex: /\*\*\n \* Licensed
    replace: /\*\n \* Licensed

These files had to be changed manually:

    artemis-selector/src/main/javacc/HyphenatedParser.jj
    artemis-selector/src/main/javacc/StrictParser.jj
    artemis-website/src/main/resources/styles/impact/css/pygmentize.css
    artemis-website/src/main/resources/styles/impact/css/site.css
2015-06-03 10:19:45 -04:00
..
src/main ARTEMIS-129 License header should be a normal comment 2015-06-03 10:19:45 -04:00
pom.xml [maven-release-plugin] prepare for next development iteration 2015-05-21 13:47:45 +01:00
readme.html Ensure all references to the project use ActiveMQ Artemis 2015-05-13 11:51:26 +01: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 Artemis 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.2 Example</h1>

     <p>This example shows you how to configure ActiveMQ Artemis to send and receive Stomp messages using Stomp 1.2 protocol.</p>
     <p>The example will start a ActiveMQ Artemis server configured with Stomp and JMS.</p>
     <p>The client will open a socket to initiate a Stomp 1.2 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.2 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 Artemis address for the JMS Queue <code>exampleQueue</code>) with a text body</li>
        <pre class="prettyprint">
          String text = "Hello World from Stomp 1.2 !";
          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>