Reading Messages in a Queue Journal

The functions used to read messages in a queue journal are the same as those used to read messages in other queues. The only difference is that the format name used to open the queue journal has a special format.

    To read messages in a queue journal
  1. Obtain the queue journal's format name.
    wsprintf( wszFormatNameBuffer,
              L"%s;JOURNAL",
              QueueFormatName
              );     
        
  2. Call MQOpenQueue to open the queue with receive access.
    QUEUEHANDLE hQueue;
    hr= MQOpenQueue(
        wszFormatNameBuffer,
        MQ_RECEIVE_ACCESS,
        0,
        &hQueue
        );
    if (FAILED(hr))
    {
     //  Handle failure
    }
        
  3. Specify the message properties to be retrieved.
    MQMSGPROPS MsgProps;
    MQPROPVARIANT aVariant[10];
    MSGPROPID aPropId[10];
    DWORD PropIdCount = 0;
    #define MSG_BODY_LEN    500
    unsigned char ucMsgBody[MSG_BODY_LEN];
    DWORD dwAppspecificIndex;
        
    // Set the PROPID_M_BODY property.
    aPropId[PropIdCount] = PROPID_M_BODY;             //PropId
    aVariant[PropIdCount].vt = VT_VECTOR|VT_UI1;      //Type
    aVariant[PropIdCount].caub.cElems = MSG_BODY_LEN; //Value
    aVariant[PropIdCount].caub.pElems = ucMsgBody;
    PropIdCount++;
        
    // Set the MQMSGPROPS structure
    MsgProps.cProp = PropIdCount;       //Number of properties.
    MsgProps.aPropID = aPropId;         //Ids of properties.
    MsgProps.aPropVar = aVariant;       //Values of properties.
    MsgProps.aStatus  = NULL;           //No Error report.
        
  4. Call MQReceiveMessage to read the first message in the queue.
    hr = MQReceiveMessage(
         hQueue,              // handle to the Queue.
         5 * 60 * 1000,       // Max time (msec) to wait for msg.
         MQ_ACTION_RECEIVE,   // Action.
         &MsgProps,           // properties to retrieve.
         NULL,                // No overlapped structure.
         NULL,                // No callback function.
         NULL,                // NO cursor.
         NULL                 // No transaction.
        );
        

Example

This example reads the first message in the queue journal. It takes the queue's identifier (GUID), translates it into a string, prepares the format name of the queue using the translated string, and opens the queue and reads the first message.

HRESULT hr;
#define FORMAT_NAME_LEN 80
WCHAR wszFormatNameBuffer[ FORMAT_NAME_LEN];
DWORD dwFormatLen = FORMAT_NAME_LEN;


wsprintf( wszFormatNameBuffer,
      L"%s;JOURNAL",
      QueueFormatName
      );     
    
///////////////////////////////////
// Open queue with receive access.
///////////////////////////////////

QUEUEHANDLE hQueue;
hr= MQOpenQueue(
    wszFormatNameBuffer,
    MQ_RECEIVE_ACCESS,
    0,
    &hQueue
    );
if (FAILED(hr))
{
 //  Handle failure
}


///////////////////////////////////
// Specify the message properties 
// you want to receive.
///////////////////////////////////

MQMSGPROPS MsgProps;
MQPROPVARIANT aVariant[10];
MSGPROPID aPropId[10];
DWORD PropIdCount = 0;
#define MSG_BODY_LEN    500
unsigned char ucMsgBody[MSG_BODY_LEN];
DWORD dwAppspecificIndex;

// Set the PROPID_M_BODY property.
aPropId[PropIdCount] = PROPID_M_BODY;             //PropId
aVariant[PropIdCount].vt = VT_VECTOR|VT_UI1;      //Type
aVariant[PropIdCount].caub.cElems = MSG_BODY_LEN; //Value
aVariant[PropIdCount].caub.pElems = ucMsgBody;
PropIdCount++;

// Set the MQMSGPROPS structure
MsgProps.cProp = PropIdCount;       //Number of properties.
MsgProps.aPropID = aPropId;         //Ids of properties.
MsgProps.aPropVar = aVariant;       //Values of properties.
MsgProps.aStatus  = NULL;           //No Error report.


//////////////////////////
// Read a message.
//////////////////////////

hr = MQReceiveMessage(
     hQueue,              // handle to the Queue.
     5 * 60 * 1000,       // Max time (msec) to wait for msg.
     MQ_ACTION_RECEIVE,   // Action.
     &MsgProps,           // properties to retrieve.
     NULL,                // No overlapped structure.
     NULL,                // No callback function.
     NULL,                // NO cursor.
     NULL                 // No transaction.
    );
if (FAILED(hr))
{
 //Handle failure
}