C example: Getting a message
This example demonstrates how to use the MQGET call to remove a message from a queue.
This extract is taken from the Browse sample application.
For IBM® MQ for z/OS® this is program CSQ4BCA1.
For the names and locations of the sample applications, see the following topics:
#include "cmqc.h"
⋮
#define BUFFERLENGTH 80
⋮
int main(int argc, char *argv[] )
{
/* */
/* Variables for MQ calls */
/* */
MQHCONN Hconn ; /* Connection handle */
MQLONG CompCode; /* Completion code */
MQLONG Reason; /* Qualifying reason */
MQHOBJ Hobj; /* Object handle */
MQMD MsgDesc = { MQMD_DEFAULT };
/* Message descriptor */
MQLONG DataLength ; /* Length of the message */
MQCHAR Buffer[BUFFERLENGTH+1];
/* Area for message data */
MQGMO GetMsgOpts = { MQGMO_DEFAULT };
/* Options which control */
/* the MQGET call */
MQLONG BufferLength = BUFFERLENGTH ;
/* Length of buffer */
⋮
/* No need to change the message descriptor */
/* (MQMD) control block because initialization */
/* default sets all the fields. */
/* */
/* Initialize the get message options (MQGMO) */
/* control block (the copy file initializes all */
/* the other fields). */
/* */
GetMsgOpts.Options = MQGMO_NO_WAIT +
MQGMO_BROWSE_FIRST +
MQGMO_ACCEPT_TRUNCATED_MSG;
/* */
/* Get the first message. */
/* Test for the output of the call is carried out */
/* in the 'for' loop. */
/* */
MQGET(Hconn,
Hobj,
&MsgDesc,
&GetMsgOpts,
BufferLength,
Buffer,
&DataLength,
&CompCode,
&Reason);
/* */
/* Process the message and get the next message, */
/* until no messages remaining. */
⋮
/* If the call fails for any other reason, */
/* print an error message showing the completion */
/* code and reason code. */
/* */
if ( (CompCode == MQCC_FAILED) &&
(Reason == MQRC_NO_MSG_AVAILABLE) )
{
⋮
}
else
{
sprintf(pBuff, MESSAGE_4_E,
ERROR_IN_MQGET, CompCode, Reason);
PrintLine(pBuff);
RetCode = CSQ4_ERROR;
}
⋮
} /* end of main */
![[UNIX, Linux, Windows, IBM i]](../common/../develop/ngmulti.gif)
![[z/OS]](../common/../develop/ngzos.gif)