Sample programs

The sample programs are:

The sample programs are located in the directories shown in Table 1. See The base directory for an explanation of mqmtop and thlqual.

Table 1. Location of sample programs

Environment
Directory containing source
Directory containing built
programs
AIX
<mqmtop>/samp
<mqmtop>/samp/bin/ia
OS/400
/QIBM/ProdData/mqm/samp/
(see note 1)
Compaq Tru64
UNIX
<mqmtop>/samp
<mqmtop>/samp/bin/ff
HP-UX
<mqmtop>/samp
<mqmtop>/samp/bin/ah and
<mqmtop>/samp/bin/hh.
(see note 2)
OS/2 Warp
<mqmtop>\tools\cplus\samples
<mqmtop>\tools\cplus\
samples\bin\i2
z/OS
thlqual.SCSQCPPS
None
Sun Solaris
<mqmtop>/samp
<mqmtop>/samp/bin/as
<mqmtop>/samp/bin/ss
Windows NT,
95, and 98
<mqmtop>\tools\cplus\samples
<mqmtop>\tools\cplus\
samples\bin\in and
<mqmtop>\tools\cplus\
samples\bin\vn
(see note 3)
Windows 3.1
(16-bit client
only)
bin\vw
bin\vw
Notes:
  1. Programs built using the ILE C++ compiler for iSeries are in the library QMQM. The include files are in /QIBM/ProdData/mqm/inc.
  2. Programs built using the HP ANSI C++ compiler are found in directory <mqmtop>/samp/bin/ah. For further information, see Compilers for WebSphere MQ platforms.
  3. Programs built using the IBM(R) VisualAge(R) for C++ for Windows V3.5 compiler are found in directory <mqmtop>\tools\cplus\samples\bin\in. Programs built using the Microsoft Visual C++(R) V4.0 are found in <mqmtop>\tools\cplus\samples\bin\vn. For further information about these compilers, see Compilers for WebSphere MQ platforms.

Sample program HELLO WORLD (imqwrld.cpp)

This program shows how to put and get a regular datagram (C structure) using the ImqMessage class. This sample uses few method invocations, taking advantage of implicit method invocations such as open, close, and disconnect.

On all platforms except z/OS

If you are using a server connection to WebSphere MQ:

  1. Run1 imqwrlds to use the existing default queue SYSTEM.DEFAULT.LOCAL.QUEUE.
  2. Run imqwrlds SYSTEM.DEFAULT.MODEL.QUEUE to use a temporary dynamically assigned queue.
Note:
If you are using a client connection to WebSphere MQ, either:
  1. Set up the MQSERVER environment variable (see the WebSphere MQ Clients manual for more information) and run imqwrldc, or
  2. Run imqwrldc queue-name queue-manager-name channel-definition where a typical channel-definition might be SYSTEM.DEF.SVRCONN/tcp/hostname (1414)

On z/OS

Sample code

Here is the code for the HELLO WORLD sample program.

extern "C" {
#include <stdio.h>
}
 
#include <imqi.hpp> // WebSphere MQ C++
 
#define EXISTING_QUEUE "SYSTEM.DEFAULT.LOCAL.QUEUE"
 
#define BUFFER_SIZE 12
 
static char gpszHello[ BUFFER_SIZE ] = "Hello world" ;
int main ( int argc, char * * argv ) {
  ImqQueueManager manager ;
  int iReturnCode = 0 ;
 
  // Connect to the queue manager.
  if ( argc > 2 ) {
    manager.setName( argv[ 2 ] );
  }
  if ( manager.connect( ) ) {
    ImqQueue * pqueue = new ImqQueue ;
    ImqMessage * pmsg = new ImqMessage ;
 
    // Identify the queue which will hold the message.
    pqueue -> setConnectionReference( manager );
    if ( argc > 1 ) {
      pqueue -> setName( argv[ 1 ] );
 
      // The named queue can be a model queue, which will result in
      // the creation of a temporary dynamic queue, which will be
      // destroyed as soon as it is closed. Therefore we must ensure
      // that such a queue is not automatically closed and reopened.
      // We do this by setting open options which will avoid the need
      // for closure and reopening.
      pqueue -> setOpenOptions( MQOO_OUTPUT | MQOO_INPUT_SHARED |
                                MQOO_INQUIRE );
    } else {
      pqueue -> setName( EXISTING_QUEUE );
 
      // The existing queue is not a model queue, and will not be
      // destroyed by automatic closure and reopening. Therefore we
      // will let the open options be selected on an as-needed basis.
      // The queue will be opened implicitly with an output option
      // during the "put", and then implicitly closed and reopened
      // with the addition of an input option during the "get".
    }
 
    // Prepare a message containing the text "Hello world".
    pmsg -> useFullBuffer( gpszHello , BUFFER_SIZE );
    pmsg -> setFormat( MQFMT_STRING );
 
    // Place the message on the queue, using default put message
    // Options.
    // The queue will be automatically opened with an output option.
    if ( pqueue -> put( * pmsg ) ) {
      ImqString strQueue( pqueue -> name( ) );
 
      // Discover the name of the queue manager.
      ImqString strQueueManagerName( manager.name( ) );
      printf( "The queue manager name is %s.\n",
              (char *)strQueueManagerName );
 
      // Show the name of the queue.
      printf( "Message sent to %s.\n", (char *)strQueue );
 
      // Retrieve the data message just sent ("Hello world" expected)
      // from the queue, using default get message options. The queue
      // is automatically closed and reopened with an input option
      // if it is not already open with an input option. We get the
      // message just sent, rather than any other message on the
      // queue, because the "put" will have set the ID of the message
      // so, as we are using the same message object, the message ID
      // acts as in the message object, a filter which says that we
      // are interested in a message only if it has this
      // particular ID.
      if ( pqueue -> get( * pmsg ) ) {
        int iDataLength = pmsg -> dataLength( );
 
        // Show the text of the received message.
        printf( "Message of length %d received, ", iDataLength );
 
        if ( pmsg -> formatIs( MQFMT_STRING ) ) {
          char * pszText = pmsg -> bufferPointer( );
 
          // If the last character of data is a null, then we can
          // assume that the data can be interpreted as a text
          // string.
          if ( ! pszText[ iDataLength - 1 ] ) {
            printf( "text is \"%s\".\n", pszText );
          } else {
            printf( "no text.\n" );
          }
 
        } else {
          printf( "non-text message.\n" );
        }
      } else {
        printf( "ImqQueue::get failed with reason code %ld\n",
                pqueue -> reasonCode( ) );
        iReturnCode = (int)pqueue -> reasonCode( );
      }
 
    } else {
      printf( "ImqQueue::open/put failed with reason code %ld\n",
              pqueue -> reasonCode( ) );
      iReturnCode = (int)pqueue -> reasonCode( );
    }
 
    // Deletion of the queue will ensure that it is closed.
    // If the queue is dynamic then it will also be destroyed.
    delete pqueue ;
    delete pmsg ;
 
  } else {
    printf( "ImqQueueManager::connect failed with reason code %ld\n"
            manager.reasonCode( ) );
    iReturnCode = (int)manager.reasonCode( );
  }
 
  // Destruction of the queue manager ensures that it is
  // disconnected.  If the queue object were still available
  // and open (which it is not), the queue would be closed
  // prior to disconnection.
 
  return iReturnCode ;
}
 

Sample programs SPUT (imqsput.cpp) and SGET (imqsget.cpp)

These programs place messages to and retrieve messages from a named queue.

On all platforms except z/OS

  1. Run imqsputs queue-name.
  2. Type in lines at the console, which are placed with WebSphere MQ as messages.
  3. Enter a null line to end the input.
  4. Run imqsgets queue-name to retrieve all the lines and display them at the console.

On z/OS

  1. Construct and run a batch job using the sample JCL imqsputr. The messages are read from the SYSIN data set.
  2. Construct and run a batch job using the sample JCL imqsgetr. The messages are retrieved from the queue and sent to the SYSPRINT data set.

See Running sample programs on z/OS for more information.

These samples show the use of the following classes:

ImqError (see ImqError)

ImqMessage (see ImqMessage)

ImqObject (see ImqObject)

ImqQueue (see ImqQueue)

ImqQueueManager (see ImqQueueManager)

Sample program DPUT (imqdput.cpp)

This is a distribution list program that puts messages to a distribution list consisting of two queues. DPUT shows the use of the ImqDistributionList class (see ImqDistributionList). This sample is not supported on z/OS.

  1. Run imqdputs queue-name-1 queue-name-2 to place messages on the two named queues.
  2. Run imqsgets queue-name-1 and imqsgets queue-name-2 to retrieve the messages from those queues.

Footnotes:

1
For details of executing C++ programs, see Appendix A, Compiling and linking.


© IBM Corporation 2001. All Rights Reserved