The Asynchronous consumption sample program

The amqscbf sample program demonstrates the use of MQCB and MQCTL to consume messages from multiple queues asynchronously.

amqscbf is provided as C source code, and a binary client and server executable on Windows, UNIX and Linux platforms.

The program is started from the command line and takes the following optional parameters:

Usage: [Options] Queue Name {queue_name}
  where Options are:
    -m Queue Manager Name
    -o Open options
    -r Reconnect Type
       d Reconnect Disabled
       r Reconnect
       m Reconnect Queue Manager

Provide more than one queue name to read messages from multiple queues (a maximum of ten queues are supported by the sample.)

Note: Reconnect type is only valid for client programs.


Example

The example shows amqscbf run as a server program reading one message from QL1 and then being stopped.

Use IBM MQ Explorer to put a test message on QL1. Stop the program by pressing enter.

C:\>amqscbf QL1
Sample AMQSCBF0 start

Press enter to end
Message Call (9 Bytes) :
Message 1

Sample AMQSCBF0 end


What amqscbf demonstrates

The sample shows how to read messages from multiple queues in the order of their arrival. This would require a lot more code using synchronous MQGET. In the case of asynchronous consumption, no polling is required, and thread and storage management is performed by IBM MQ. A "real world" example would need to deal with errors; in the sample errors are written out to the console.

The sample code has the following steps,
  1. Define the single message consumption callback function,
    void MessageConsumer(MQHCONN      hConn,
                            MQMD    * pMsgDesc,
                            MQGMO   * pGetMsgOpts,
                            MQBYTE  * Buffer,
                            MQCBC   * pContext)
    { ... }
    
  2. Connect to the queue manager,
    MQCONNX(QMName,&cno,&Hcon,&CompCode,&CReason);
    
  3. Open the input queues, and associate each one with the MessageConsumer callback function,
    MQOPEN(Hcon,&od,O_options,&Hobj,&OpenCode,&Reason);
    cbd.CallbackFunction = MessageConsumer;
    MQCB(Hcon,MQOP_REGISTER,&cbd,Hobj,&md,&gmo,&CompCode,&Reason);
    
    cbd.CallbackFunction does not need to be set for each queue; it is an input-only field. But you could associate a different callback function with each queue.
  4. Start consumption of the messages,
    MQCTL(Hcon,MQOP_START,&ctlo,&CompCode,&Reason);
    
  5. Wait until the user has pressed enter and then stop consumption of messages,
    MQCTL(Hcon,MQOP_STOP,&ctlo,&CompCode,&Reason);
    
  6. Finally disconnect from the queue manager,
    MQDISC(&Hcon,&CompCode,&Reason);
    

Parent topic: Use the sample programs on Multiplatforms