Implement request response with JMS Temporary Queues
Temporary destinations (temporary queues or temporary topics) are proposed as a lightweight alternative in a scalable system architecture that could be used as unique destinations for replies. Such destinations have a scope limited to the connection that created it, and are removed on the server side as soon as the connection is closed. Only a single well-known static queue is required for producers/senders to connect with consumers using temporary destinations. The JMS header field JMSReplyTo is always used in conjunction with temporary destinations.
Since the identity of the temporary destination is known only by the connection or session that created it, the consumer/receiver cannot know the destination name. The solution is to have the producer/sender send the name of its temporary destination as a header field (JMSReplyTo), as part of a message, sent to a known static queue listened to by the producer/sender.
Limitations of Temporary Destinations
There are a few limitations that must be recognized regarding temporary destinations:
MessageProducer producer = session.createProducer(null);
TextMessage message = session.createTextMessage();
message.setText("Hello");
installProperties(message);
//this temporary queue can be accessed by the session it created./
TemporaryQueue replyToDestination = session.createTemporaryQueue();
message.setJMSReplyTo(replyToDestination);
producer.send(destination, message);
//on the same session create consumer
MessageConsumer consumer=session.createConsumer(replyToDestination )
//receive method specifies the timeout second
TextMessage replyMessage = (TextMessage)consumer.receive(timeoutInMillSecs);
if(replyMessage != null)
String reply = replyMessage.getText();
}
//delete the temp queue using delete method
finally
{
if(consumer != null)
try
{
consumer.close();
}
catch(Exception excep) { }
try
{
replyToDestination .delete();
}
catch(Exception exception) { }
}
Since the identity of the temporary destination is known only by the connection or session that created it, the consumer/receiver cannot know the destination name. The solution is to have the producer/sender send the name of its temporary destination as a header field (JMSReplyTo), as part of a message, sent to a known static queue listened to by the producer/sender.
Limitations of Temporary Destinations
There are a few limitations that must be recognized regarding temporary destinations:
- A temporary destination can only be consumed by the connection that created it.
- The life span of temporary destinations is only for the duration of the connection where they are created.
- When you close the connection that has a temporary destination, the destination is closed and its contents are lost.
- You cannot have durable subscriptions to a TemporaryTopic.
- Each temporary destination is unique and cannot be copied.
- Temporary destinations cannot be routed using an enterprise messaging service.
MessageProducer producer = session.createProducer(null);
TextMessage message = session.createTextMessage();
message.setText("Hello");
installProperties(message);
//this temporary queue can be accessed by the session it created./
TemporaryQueue replyToDestination = session.createTemporaryQueue();
message.setJMSReplyTo(replyToDestination);
producer.send(destination, message);
//on the same session create consumer
MessageConsumer consumer=session.createConsumer(replyToDestination )
//receive method specifies the timeout second
TextMessage replyMessage = (TextMessage)consumer.receive(timeoutInMillSecs);
if(replyMessage != null)
String reply = replyMessage.getText();
}
//delete the temp queue using delete method
finally
{
if(consumer != null)
try
{
consumer.close();
}
catch(Exception excep) { }
try
{
replyToDestination .delete();
}
catch(Exception exception) { }
}
Comments
Post a Comment