Design of the sample

 

The Print message sample application uses a single program written in the C language.

The flow through the program logic is:

  1. Check that the names of the queue manager and queue have been passed from the run JCL. If they have not, print an error message and stop processing.

  2. Connect to the queue manager using the MQCONN call. If this call is not successful, print the completion and reason codes and stop processing; otherwise print the name of the queue manager.

  3. Open the queue using the MQOPEN call with the MQOO_INPUT_SHARED option.

    If you want the application to browse the messages rather than remove them from the queue, compile the sample with -DBROWSE, or, add #define BROWSE at the top of the source. When you do this, the macro preprocessor adds the line in the program that selects the MQOO_BROWSE option in the compilation.

    On input to this call, the program uses the connection handle returned in step 2. For the object descriptor structure (MQOD), it uses the default values for all the fields except the queue name (which was passed in step 1). If this call is not successful, print the completion and reason codes and stop processing; otherwise, print the name of the queue.

  4. Perform the following loop until the MQGET call fails:

    1. Initialize the buffer to blanks so that the message data does not get corrupted by any data already in the buffer.

    2. Set the MsgId and CorrelId fields of the MQMD structure to nulls so that the MQGET call selects the first message from the queue.

    3. Get a message from the queue, using the MQGET call. On input to this call, the program specifies:

      • The connection and object handles from steps 2 and 3.

      • An MQMD structure with all fields set to their initial values. (MsgId and CorrelId are reset to nulls for each MQGET call.)

      • The option MQGMO_NO_WAIT.

        If you want the application to browse the messages rather than remove them from the queue, compile the sample with -DBROWSE, or, add #define BROWSE at the top of the source. When you do this, the macro preprocessor adds the line in the program that selects the MQGMO_BROWSE_NEXT option to the compilation. When this option is used on a call against a queue for which no browse cursor has previously been used with the current object handle, the browse cursor is positioned logically before the first message.

      • A buffer of size 32 KB to hold the data copied from the message.

    4. Call the printMD subroutine. This prints the name of each field in the message descriptor, followed by its contents.

    5. Print the length of the message, followed by the message data. Each line of message data is in this format:

      • Relative position (in hexadecimal) of this part of the data

      • 16 bytes of hexadecimal data

      • The same 16 bytes of data in character format, if it is printable (nonprintable characters are replaced by periods)

  5. If the MQGET call fails, test the reason code to see if the call failed because there are no more messages on the queue. In this case, print the message: No more messages; otherwise, print the completion and reason codes. In both cases, go to step 6.

    The MQGET call fails if it finds a message that has more than 32 KB of data. To change the program to handle larger messages, you could do one of the following:

    • Add the MQGMO_ACCEPT_TRUNCATED_MSG option to the MQGET call, so that the call gets the first 32 KB of data and discards the remainder

    • Make the program leave the message on the queue when it finds one with this amount of data

    • Increase the size of the buffer

  6. Close the queue using the MQCLOSE call with the object handle returned in step 3.

  7. Disconnect from the queue manager using the MQDISC call with the connection handle returned in step 2.

 

Parent topic:

The Print Message sample


fg18370_