Sample program HELLO WORLD (imqwrld.cpp)

This C++ sample program shows how to put and get a regular datagram (C structure) using the ImqMessage class.

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, follow one of the following procedures:
  • To use the existing default queue, SYSTEM.DEFAULT.LOCAL.QUEUE, run the program imqwrlds without passing any parameters
  • To use a temporary dynamically assigned queue, run imqwrlds passing the name of the default model queue, SYSTEM.DEFAULT.MODEL.QUEUE.
If you are using a client connection to WebSphere MQ, follow one of the following procedures:
  • Set up the MQSERVER environment variable (see MQSERVER for more information) and run imqwrldc, or
  • Run imqwrldc passing as parameters the queue-name, queue-manager-name, and channel-definition, where a typical channel-definition might be SYSTEM.DEF.SVRCONN/TCP/hostname(1414)

Sample code


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 ;
}