Loosely coupled applications require special techniques when it is important for the publisher to certify that a message was delivered in either messaging domain:
Publish and Subscribe — While the publisher can send long-lived messages to durable receivers and get acknowledgement from the broker, neither of these techniques confirms that a message was actually delivered or how many, if any, subscribers received the message.
Point-to-point — While a sender can see if a message was removed from a queue, implying that it was delivered, there is no indication where it went.
A message producer can request a reply when a message is sent. A common way to do this is to set up a temporary destination and header information that the consumer can use to create a reply to the sender of the original message.
In both request and reply samples, the replier’s task is a simple data processing exercise: standardize the case of the text sent—receive text and send back the same text as either all uppercase characters or all lowercase characters—then publish the modified message to the temporary destination that was set up for the reply.
While request-and-reply provides proof of delivery, it is a blocking transaction—the requestor waits until the reply arrives. While this situation might be appropriate for a system that, for example, issues lottery tickets, it might be preferable in other situations to have a formally established return destination that echoes the original message and a correlation identifier—a designated identifier that certifies that each reply is referred to its original requestor.
The sample applications use JMS sample classes
TopicRequestor
and
QueueRequestor
. You should create the Request/Reply
helper classes that are appropriate for your application.
These request and reply samples show that request/reply mechanisms are very similar across messaging models, and that, while there might be zero or many subscriber replies, there will be, at most, one PTP reply.
In this example in the Pub/Sub domain, the replier application must be started before the requestor so that the Pub/Sub replier’s message listener can receive the message and release the blocked requestor.
To run the Pub/Sub Request and Reply sample do the following:
In window 1, enter ant trequest
.
In window 2, enter ant treply
.
In the Requestor window, type
AaBbCc
then press
Enter.
The Replier window reflects the activity, displaying:
[Request] RequestingChatter:
AaBbCc
The replier completes its operation (converts text to uppercase) and sends the result in a message to the requestor. The requestor gets the reply from the replier:
[Reply] Transformed RequestingChatter: AaBbCc to all uppercase: REQUESTINGCHATTER: AABBCC
In the PTP domain, the requestor application can be started and even
send a message before the replier application is started. The queue
holds the message until the replier is available. The requestor is still
blocked, but when the replier’s message listener receives the message,
it releases the blocked requestor. The sample code includes an option
(-m
) to switch the mode between uppercase and
lowercase.
To run the PTP Request and Reply sessions do the following:
In window 1, enter: ant
qrequest
.
In window 2, enter: ant qreply
.
In the Requestor
window, type
AaBbCc
then press
Enter.
The Replier window reflects the activity, displaying:
[Request] RequestingTalker:
AaBbCc
The replier completes its operation (converts text to uppercase) and sends the result in a message to the requestor. The requestor gets the reply from the replier:
[Reply] Transformed RequestingTalker: AaBbCc to all uppercase: REQUESTINGTALKER: AABBCC