Introduction
In an environment where many applications/devices run on different OS/infrastructure and communicate through messaging using WebSphere MQ as middleware, there is always some chance of messages getting lost due to network problems or due to application crashes.
We must handle those exceptions in our application to make sure that we do not lose any message.
Here are some practical examples of problems and their solutions that I have faced while working on some big Financial Applications.
If an application throws some type of exception (Business logic failure, DB failure) after reading messages from Queues, how are you going to handle the message that you already read?
How to handle a proper asynchronous request/reply communication, to make sure that we do not misunderstand a reply for a specific request?
What if WebSphere Queue Manager crashed out after you put messages on a queue?
Background
Syncpoint Coordination
Syncpoint coordination is the process by which units of work are either committed or backed out with data integrity.
You must write your application to handle Synch Point Commit and Back out so that if anything fails, you would be able to easily back out the message.
Correlation ID
A correlation ID is usually put in the header of a message. The ID is not part of the command or data the caller is trying to communicate to the receiver.
The receiver saves the ID from the request and adds it to the reply for the caller's benefit. Since the message body is the content being transmitted between the two systems, and the ID is not a part of that, the ID is added to the header.
In the request message, the ID can be stored as a correlation ID property or simply a message ID property. When used as a correlation ID, this can cause confusion about which message is the request and which is the reply. If a request has a message ID but no correlation ID, then a reply has a correlation ID that is the same as the request's message ID.
You must write your application to read write messages using correlation id in an asynchronous request/reply environment.
Using the Code
Given below is the C++ code for reading/writing messages from/to queues using Synch Point Commit, Back Out and Correlation ID...
void put_request()
{
ImqQueue request_queue; ImqMessage msg ; ImqPutMessageOptions pmo;
pmo.setSyncPointParticipation(true);
msg.setMessageId();
msg.setPersistence(MQPER_PERSISTENT);
request_queue.put( msg, pmo );
ImqBinary pID( msg.messageId() );
pID.copyOut( &corrID[0], 128 );
request_queue.close();
}
void get_reply()
{
ImqQueue reply_queue; ImqMessage msg ; ImqGetMessageOptions GMO;
GMO.setSyncPointParticipation(true);
msg.setCorrelationId(corrID);
reply_queue.get(msg, GMO));
reply_queue.close();
}
void get_request()
{
ImqQueue request_queue; ImqQueue reply_queue; ImqGetMessageOptions GMO;
GMO.setSyncPointParticipation(true);
request_queue.get(msg,GMO);
if()
{
reply_msg.setCorrelationId(msg.messageId());
reply_queue.put(reply_msg);
}
else if ()
{
mgr.backout();
}
else
{
}
}