Specifying the size of the buffer area

 

In the BufferLength parameter of the MQGET call, specify the size of the buffer area to hold the message data that you retrieve. You decide how big this should be in three ways:

  1. You might already know what length of messages to expect from this program. If so, specify a buffer of this size.

    However, we can use the MQGMO_ACCEPT_TRUNCATED_MSG option in the MQGMO structure if you want the MQGET call to complete even if the message is too big for the buffer. In this case:

    • The buffer is filled with as much of the message as it can hold

    • The call returns a warning completion code

    • The message is removed from the queue (discarding the remainder of the message), or the browse cursor is advanced (if you are browsing the queue)

    • The real length of the message is returned in DataLength

    Without this option, the call still completes with a warning, but it does not remove the message from the queue (or advance the browse cursor).

  2. Estimate a size for the buffer (or even specify a size of zero bytes) and do not use the MQGMO_ACCEPT_TRUNCATED_MSG option. If the MQGET call fails (for example, because the buffer is too small), the length of the message is returned in the DataLength parameter of the call. (The buffer is still filled with as much of the message as it can hold, but the processing of the call is not completed.) Store the MsgId of this message, then repeat the MQGET call, specifying a buffer area of the correct size, and the MsgId that you noted from the first call.

    If your program is serving a queue that is also being served by other programs, one of those other programs might remove the message that you want before your program can issue another MQGET call. Your program could waste time searching for a message that no longer exists. To avoid this, first browse the queue until you find the message that you want, specifying a BufferLength of zero and using the MQGMO_ACCEPT_TRUNCATED_MSG option. This positions the browse cursor under the message that you want. We can then retrieve the message by calling MQGET again, specifying the MQGMO_MSG_UNDER_CURSOR option. If another program removes the message between your browse and removal calls, your second MQGET fails immediately (without searching the whole queue), because there is no message under your browse cursor.

  3. The MaxMsgLength queue attribute determines the maximum length of messages accepted for that queue; the MaxMsgLength queue manager attribute determines the maximum length of messages accepted for that queue manager. If you do not know what length of message to expect, we can inquire about the MaxMsgLength attribute (using the MQINQ call), then specify a buffer of this size.

    Try to make the buffer size as close as possible to the actual message size to avoid reduced performance.

    For further information about the MaxMsgLength attribute, see Increasing the maximum message length.

 

Parent topic:

Getting messages from a queue using the MQGET call


fg12590_