Stomp 1.1 Example

This example shows you how to configure ActiveMQ Artemis to send and receive Stomp messages using Stomp 1.1 protocol.

The example will start a ActiveMQ Artemis server configured with Stomp and JMS.

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.

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. We create a TCP socket to connect to the Stomp port
              Socket socket = new Socket("localhost", 61613); 
            
  2. We negotiate a Stomp 1.1 connection to the server
  3.          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);
            
  4. We receive a response showing that the connection version
  5.          String response = receiveFrame(socket);
             System.out.println("response: " + response);
            
  6. We send a SEND frame (a Stomp message) to the destination jms.queue.exampleQueue (which corresponds to the ActiveMQ Artemis address for the JMS Queue exampleQueue) with a text body
  7.           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);
            
  8. We send a DISCONNECT frame to disconnect from the server
  9.           String disconnectFrame = "DISCONNECT\n" +
                 "\n" +
                 Stomp.NULL;
              sendFrame(socket, disconnectFrame);
            
  10. We close the TCP socket
  11.           socket.close();
            
  12. We create an initial context to perform the JNDI lookup.
  13.           initialContext = getContext(0);
           
  14. We perform a lookup on the queue and the connection factory
  15.           Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
              ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
            
  16. We create a JMS Connection, Session and a MessageConsumer on the queue
  17.           connection = cf.createConnection();
              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
              MessageConsumer consumer = session.createConsumer(queue);
            
  18. We start the connection
  19.            connection.start();
            
  20. We receive the message. Stomp messages are mapped to JMS TextMessage.
  21.           TextMessage messageReceived = (TextMessage)consumer.receive(5000);
              System.out.println("Received JMS message: " + messageReceived.getText());
            
  22. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  23.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }