watangka
watangka

Reputation: 101

WMQ C++ Get request's message id and set it correlation id for reply

I have a problem on getting message id and set it on reply's correlation id.

Here's the piece of code:

MQBYTE msgID;

request_msg.messageId().copyOut(msgID, MQ_MSG_ID_LENGTH, 0);
response_msg.setCorrelationId(msgID);

when I checked the correlation Id of the reply, the correlation id is 0.

How can I copy/get the messageId of the request and put it on the correlation id of the reply?

Thanks in advance. :)

Upvotes: 3

Views: 1037

Answers (1)

T.Rob
T.Rob

Reputation: 31852

The Infocenter page Manipulating binary strings in C++ shows the data types involved as ImqBinary and MQBYTE24. As Shashi notes in the comments, MQBYTE is a single byte and incapable of containing a 24-byte message ID. The page linked above provides a reference example:

#include <imqi.hpp> // C++ classes

ImqMessage message ;
ImqBinary id, correlationId ;
MQBYTE24 byteId ;

correlationId.set( byteId, sizeof( byteId ) ); // Set.
id = message.id( );                            // Assign.
if ( correlationId == id ) {                   // Compare.
  ...

One tool to diagnose issues like this is the very useful SupportPac MA0W which is an API exit. It can show you the exact contents of the structures passed to MQ before and after an API call. Very often what is seen is that what is expected (in this case having copied the MQMD.MsgID to the MQMD.CorrelID is not what actually happened. In this case, I believe a trace such as that provided by MA0W would reveal that the msgID was accurately passed from the app to MQ but consisted of only one character.

UPDATE
OP asks:

Variable id is an ImqBinary and message is an ImqMessage object, from what I know id is not a member of ImqMessage like messageId, correlationId, groupId and etc. so how can message.id() passed it's value on id?

You are coorrect that id is a declared variable of type ImqBinary and not a member of the ImqMessage class. Please see the Infocenter page for the ImqBinary C++ class which explains that:

This class encapsulates a binary byte array that can be used for ImqMessage accounting token, correlation id, and message id values. It allows easy assignment, copying, and comparison.

The intent of the ImqBinary class is to provide a variable type to encapsulate a byte array with overloaded methods such that "normal" variable manipulations work as expected. Rather than copying the byte array one byte at a time, it can be an LVAL or RVAL in an assignment. Rather than comparing the array a byte at a time, you can just use comparison operators like ==.

So if your code were modified to use the example, it might look like this:

#include <imqi.hpp> // C++ classes

ImqMessage request_msg, response_msg ;
ImqBinary id ;

id = request_msg.id( );
response_msg.setCorrelationId(id);

That said, I'm not sure you even need an intermediate variable. You might be able to assign the correlation ID using the output of the getter call for the message ID from the source message. Something like....

response_msg.setCorrelationId( request_msg.id( ) );

...might do it. I don't code C or C++ anymore so I probably got the syntax wrong or didn't do it as elegantly as it might be coded, but you should get the idea.

Upvotes: 4

Related Questions