This example shows you how to send and receive a message to a JMS Queue using ActiveMQ by using a JMS Context
A JMSContext is part of JMS 2.0 and combines the JMS Connection and Session Objects into a simple Interface
To run the example, simply type mvn verify
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
jmsContext = cf.createContext();
jmsContext.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT).send(queue, "this is a string")
String payLoad = jmsContext.createConsumer(queue).receiveBody(String.class);
TextMessage message = session.createTextMessage("This is a text message");
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (jmsContext != null)
{
jmsContext.close();
}
}