The Print Message sample

The Print Message sample is a simple batch application that demonstrates how to remove all the messages from a queue using the MQGET call. It also prints, for each message, the fields of the message descriptor, followed by the message data. The program prints the data both in hexadecimal and as characters (if they are printable). If a character is not printable, the program replaces it with a period character (.). You can use the program when diagnosing problems with an application that is putting messages on a queue.

You can change the application so that it browses the messages, rather than removing them from the queue. To do this, remove the comment characters from two lines in the code, as indicated in Design of the sample.

The application has a single source program, which is written in the C language. Sample run JCL code is also supplied (see Table 47).

To start the application, you must edit and run the sample run JCL, as described in Preparing and running sample applications for the batch environment. When you run the application (and there are some messages on the queue), the output data set looks like that in Figure 42.

Figure 42. Example of a report from the Print Message sample application


 MQCONN to VC4
 MQOPEN - 'CSQ4SAMP.DEAD.QUEUE'
 
 
 MQGET of message number 1
****Message descriptor****
  StrucId  : 'MD  '  Version : 1
  Report   : 0  MsgType : 2
  Expiry   : -1  Feedback : 0
  Encoding : 785  CodedCharSetId : 500
  Format : '        '
  Priority : 3  Persistence : 0
  MsgId : X'C3E2D840E5C3F4404040404040404040A6FE06A95105C620'
  CorrelId : X'C3E2D840E5C3F4404040404040404040A6FE062950C2F125'
  BackoutCount : 0
  ReplyToQ       : '                                                '
  ReplyToQMgr    : 'VC4                                             '
  ** Identity Context
  UserIdentifier : 'CICSUSER    '
  Account.Token :
   X'160DD5E3E2D5C5E34BC9C7D7C2F6F1FE060D3B55B60001000000000000000000'
  ApplIdentData  : '                                '
  ** Origin Context
  PutApplType    : '1'
  PutApplName    : 'VICAUT4 MVB5                '
  PutDate  : '19930203'    PutTime  : '20165982'
  ApplOriginData : '    '
****   Message      ****
 length - 429 bytes
 
00000000:  C3E2 D8F4 C2D8 D9D4 4040 4040 4040 4040 'CSQ4BQRM        '
00000010:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000020:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000030:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000040:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000050:  4040 4040 4040 40D1 D6C8 D540 D140 4040 '       JOHN J   '
00000060:  4040 4040 4040 4040 4040 40F1 F2F3 F4F5 '           12345'
00000070:  F6F7 F8F9 C6C9 D9E2 E340 C7C1 D3C1 C3E3 '6789FIRST GALACT'
00000080:  C9C3 40C2 C1D5 D240 4040 4040 4040 4040 'IC BANK         '
00000090:  4040 E2D6 D4C5 E3C8 C9D5 C740 C4C9 C6C6 '  SOMETHING DIFF'
000000A0:  C5D9 C5D5 E340 4040 4040 4040 4040 4040 'ERENT           '
000000B0:  F3F5 F0F1 F6F7 F6F2 F1F2 F1F0 F0F0 F0F0 '3501676212100000'
000000C0:  D985 A297 9695 A285 4086 9996 9440 C3E2 'Response from CS'
000000D0:  D8F4 E2C1 D4D7 4BC2 F74B D4C5 E2E2 C1C7 'Q4SAMP.B7.MESSAG'
000000E0:  C5E2 4040 4040 4040 4040 4040 4040 4040 'ES              '
000000F0:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000100:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000110:  4040 4040 40D3 9681 9540 8194 96A4 95A3 '     Loan amount'
00000120:  40F1 F0F0 F0F0 F040 8696 9940 D1D6 C8D5 ' 100000 for JOHN'
00000130:  40D1 4040 4040 4040 4040 4040 4040 4040 ' J              '
00000140:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000150:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
00000160:  4040 4040 C399 8584 89A3 40A6 9699 A388 '    Credit worth'
00000170:  8995 85A2 A240 8995 8485 A740 6040 C2C1 'iness index - BA'
00000180:  C440 4040 4040 4040 4040 4040 4040 4040 'D               '
00000190:  4040 4040 4040 4040 4040 4040 4040 4040 '                '
000001A0:  4040 4040 4040 4040 4040 4040 40        '                '
 
 
 No more messages
MQCLOSE
 MQDISC

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 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.
    Note:
    If you want the application to browse the messages rather than remove them from the queue, remove the comment characters from the line in the program that adds the MQOO_BROWSE option.

    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.
      • A MQMD structure with all fields set to their initial values. (Note that MsgId and CorrelId are reset to nulls for each MQGET call.)
      • The option MQGMO_NO_WAIT.
        Note:
        If you want the application to browse the messages rather than remove them from the queue, remove the comment characters from the line in the program that adds the MQOO_BROWSE_NEXT option. 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.
    Note:
    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.


© IBM Corporation 1993, 2002. All Rights Reserved