JMS OpenWire Example

This example shows you how to configure ActiveMQ server to communicate with an ActiveMQ JMS client using ActiveMQ's native openwire protocol.

Example step-by-step

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

  1. First we need to create an ActiveMQ connection factory.
  2.            ConnectionFactory factory = new ActiveMQConnectionFactory(urlString);
            
  3. Create the target queue
  4.            Queue queue = new ActiveMQQueue("exampleQueue");
            
  5. We create a JMS connection and start it
  6.            connection = cf.createConnection();
               connection.start()
            
  7. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  8.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  9. We create a JMS message producer on the session. This will be used to send the messages.
  10.           MessageProducer messageProducer = session.createProducer(topic);
           
  11. We create a JMS text message that we are going to send.
  12.            TextMessage message = session.createTextMessage("This is a text message");
            
  13. We send message to the queue
  14.            messageProducer.send(message);
            
  15. We create a JMS Message Consumer to receive the message.
  16.            MessageConsumer messageConsumer = session.createConsumer(queue);
            
  17. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'
  18.            TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
            
  19. 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
  20.            finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
               }