
examples.jms.temp-queue.readme.html Maven / Gradle / Ivy
HornetQ JMS Temporary Queue Example
JMS Temporary Queue Example
This example shows you how to use a TemporaryQueue with HornetQ. First a temporary queue is created to send and receive a message and then deleted.
Then another temporary queue is created and used after its connection is closed to illustrate its scope.
A TemporaryQueue is a JMS queue that exists only within the lifetime of its connection. It is often used in request-reply
type messaging where the reply is sent through a temporary destination. The temporary queue is often created as
a server resource, so after using, the user should call delete() method to release the resources.
Please consult the JMS 1.1 specification for full details.
Example step-by-step
To run the example, simply type ./build.sh
(or build.bat
on windows) from this directory
- First we need to get an initial context so we can look-up the JMS connection factory from JNDI. This initial context will get it's properties from the
client-jndi.properties
file in the directory ../common/config
initialContext = getContext();
- We look-up JMS connection factory from JNDI
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
- We Create a JMS connection
connection = cf.createConnection();
- We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
connection.start();
- We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- We create a Temporary Queue
Queue tempQueue = session.createTemporaryQueue();
- We create a JMS message producer to the temporary queue. This will be used to send the messages.
MessageProducer messageProducer = session.createProducer(tempQueue);
- We create a JMS text message to send
TextMessage message = session.createTextMessage("This is a text message");
- We send the message to the temporary queue
messageProducer.send(message);
- We create a message consumer of the temporary queue
MessageConsumer messageConsumer = session.createConsumer(tempQueue);
- We receive the message from the temporary queue
message = (TextMessage) messageConsumer.receive(5000);
- We close the consumer and producer before destroying the temporary queue
messageConsumer.close();
messageProducer.close();
- We delete the temporary queue
tempQueue.delete();
- We create another temporary queue
TemporaryQueue tempQueue2 = session.createTemporaryQueue();
- We close the connection
connection.close();
- We create a new connection
connection = cf.createConnection();
- We create a new session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- We try to access the tempQueue2 outside its lifetime, this will cause exception thrown
try
{
messageConsumer = session.createConsumer(tempQueue2);
throw new Exception("Temporary queue cannot be accessed outside its lifecycle!");
}
catch (InvalidDestinationException e)
{
System.out.println("Exception got when trying to access a temp queue outside its scope: " + e);
}
- 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
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}