The Get sample programs

The Get sample programs get messages from a queue using the MQGET call. See Features demonstrated in the sample programs for the names of these programs.

Running the amqsget and amqsgetc samples

These programs each take two parameters:

  1. The name of the source queue (required)
  2. The name of the queue manager (optional)

If a queue manager is not specified, amqsget connects to the default queue manager, and amqsgetc connects to the queue manager identified by an environment variable or the client channel definition file.

To run these programs, enter one of the following:

amqsget myqueue qmanagername

amqsgetc myqueue qmanagername

where myqueue is the name of the queue from which the program will get messages, and qmanagername is the queue manager that owns myqueue.

If you omit the qmanagername, the programs assume the default, or, in the case of the MQI client, the queue manager identified by an environment variable or the client channel definition file.

Running the amqsgetw sample

This program has no visible interface, all messages are written to the output file, not to stdout.

This program takes 3 parameters:

  1. The name of the output file (required)
  2. The name of the queue manager (required)
  3. The name of the target queue (optional)

To run amqsgetw from a command prompt, enter the program name followed by the parameters.

For example:

  amqsgetw outfile.out qmanagername myqueue

where:

outfile.out is used to hold the messages generated when the program runs.

qmanagername is the queue manager that owns myqueue.

myqueue is the name of the target queue from which the program will get messages. If you do not enter a queue name, the default queue for the queue manager is used.

Here is an example of the contents of the output file:

Sample AMQSGETW start
Output file "OUTFILE.OUT" opened
Queue Manager name "QMANAGERNAME" will be used
Queue Name "MYQUEUE" will be used
MQGET OK - message contents: <AMQSPUTW: Windows Client Test Message 1>
MQGET OK - message contents: <AMQSPUTW: Windows Client Test Message 2>
MQGET OK - message contents: <AMQSPUTW: Windows Client Test Message 3>
no more messages
Sample AMQSGETW end

It is important always to look in the output file to see what has happened as there is no visible indication of success or failure when you run this program.

Running the amq0get sample

The COBOL version does not have any parameters. It connects to the default queue manager and when you run it you are prompted:

Please enter the name of the source queue

Each program removes messages from the queue you specify when you call the program. You could use the supplied queue SYSTEM.SAMPLE.LOCAL; run the Put sample program first to put some messages on the queue. You could use the queue SYSTEM.SAMPLE.ALIAS, which is an alias name for the same local queue. The program continues until the queue is empty or an MQI call fails.

Running the AMQSGET4 and the AMQ0GET4 samples

The Get sample programs get messages from a queue using the MQGET call. The programs are named:

C language AMQSGET4
COBOL language AMQ0GET4

Each program removes messages from the queue you specify when you call the program. You could use the supplied queue SYSTEM.SAMPLE.LOCAL; run the Put sample program first to put some messages on the queue. You could use the queue SYSTEM.SAMPLE.ALIAS, which is an alias name for the same local queue. The program continues until the queue is empty or an MQI call fails.

An example of a command to call the C program is:

   CALL PGM(QMQM/AMQSGET4) PARM('SYSTEM.SAMPLE.LOCAL')

Design of the Get sample program

The program opens the target queue using the MQOPEN call with the MQOO_INPUT_AS_Q_DEF option. If it cannot open the queue, the program displays an error message containing the reason code returned by the MQOPEN call.

For each message on the queue, the program uses the MQGET call to remove the message from the queue, then displays the data contained in the message. The MQGET call uses the MQGMO_WAIT option, specifying a WaitInterval of 15 seconds, so that the program waits for this period if there is no message on the queue. If no message arrives before this interval expires, the call fails and returns the MQRC_NO_MSG_AVAILABLE reason code.

The program demonstrates how you must clear the MsgId and CorrelId fields of the MQMD structure after each MQGET call because the call sets these fields to the values contained in the message it retrieves. Clearing these fields means that successive MQGET calls retrieve messages in the order in which the messages are held in the queue.

The MQGET call specifies a buffer of fixed size. If a message is longer than this buffer, the call fails and the program stops.

The program continues until either the MQGET call returns the MQRC_NO_MSG_AVAILABLE reason code or the MQGET call fails. If the call fails, the program displays an error message that contains the reason code.

The program then closes the queue using the MQCLOSE call.



© IBM Corporation 1993, 2002. All Rights Reserved