Stomp Example

This example shows you how to configure ActiveMQ to send and receive Stomp messages.

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

The client will open a socket to 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 send a CONNECT frame to connect to the server
  3.           String connectFrame = "CONNECT\n" +
                 "login: guest\n" + 
                 "passcode: guest\n" + 
                 "request-id: 1\n" + 
                 "\n" +
                 Stomp.NULL;
              sendFrame(socket, connectFrame);
            
  4. We send a SEND frame (a Stomp message) to the destination jms.queue.exampleQueue (which corresponds to the ActiveMQ address for the JMS Queue exampleQueue) with a text body
  5.           String text = "Hello, world from Stomp!";
              String message = "SEND\n" + 
                 "destination: jms.queue.exampleQueue\n" +
                 "\n" +
                 text +
                 Stomp.NULL;
              sendFrame(socket, message);
              System.out.println("Sent Stomp message: " + text);
            
  6. We send a DISCONNECT frame to disconnect from the server
  7.           String disconnectFrame = "DISCONNECT\n" +
                 "\n" +
                 Stomp.NULL;
              sendFrame(socket, disconnectFrame);
            
  8. We close the TCP socket
  9.           socket.close();
            
  10. We create an initial context to perform the JNDI lookup.
  11.           initialContext = getContext(0);
           
  12. We perform a lookup on the queue and the connection factory
  13.           Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
              ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
            
  14. We create a JMS Connection, Session and a MessageConsumer on the queue
  15.           connection = cf.createConnection();
              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
              MessageConsumer consumer = session.createConsumer(queue);
            
  16. We start the connection
  17.            connection.start();
            
  18. We receive the message. Stomp messages are mapped to JMS TextMessage.
  19.           TextMessage messageReceived = (TextMessage)consumer.receive(5000);
              System.out.println("Received JMS message: " + messageReceived.getText());
            
  20. 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
  21.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }