メッセージキューイングアクセス機能 TP1/Message Queue - Access 使用の手引

[目次][用語][索引][前へ][次へ]

C++のサンプルコーディング

コーディング例を示します。

 
#include <stdio.h>
#include <imqi.hpp>          // include MQAccess C++ class
 
int main (void) {
  ImqQueueManager  mgr(" ");      /* queue manager name */
  ImqQueue       queue;           /* queue */
  ImqMessage     msg;             /* message */
  ImqString      str("****** C++ sample put data ******");
                                  /* message data */
  ImqGetMessageOptions getoption; /* get message options */
  ImqPutMessageOptions putoption; /* put message options */
 
  // connect to the queue manager
  if ( ! mgr.connect( ) ) {
    /* stop if it failed */
    printf(
    "ImqQueueManager::connect ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
    return ( (int)mgr.reasonCode( ) );
  }
 
  // set the connection reference
  queue.setConnectionReference( mgr );
 
  // set the queue name
  queue.setName("dynq1");
 
  // set the message
  msg.writeItem( str );
 
  // set the open options
  queue.setOpenOptions(MQOO_OUTPUT | MQOO_INPUT_AS_Q_DEF);
 
  // open the queue
  queue.open( );
  if ( queue.reasonCode( ) ) {
    printf( "ImqQueue::open ended with reason code %d\n",
                               (int)queue.reasonCode( ) );
    return( (int)queue.reasonCode( ) );
  }
 
  // set the get message options
  getoption.setSyncPointParticipation(TRUE);
  // set the put message options
  putoption.setSyncPointParticipation(TRUE);
 
  // begin local transaction
  if ( ! mgr.begin( ) ) {
    printf(
    "ImqQueueManager::begin ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
    return( (int)mgr.reasonCode( ) );
  }
 
  // put the message
  if ( ! queue.put( msg, putoption ) ) {
    printf( "ImqQueue::put ended with reason code %d\n",
                                (int)queue.reasonCode( ) );
  }
 
  // commit local transaction
  if ( queue.reasonCode( ) == 0 ) {
    if ( ! mgr.commit( ) ) {
      printf(
     "ImqQueueManager::commit ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
      return( (int)mgr.reasonCode( ) );
    }
  } else {
    if ( ! mgr.backout( ) ) {
      printf(
    "ImqQueueManager::backout ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
      return( (int)mgr.reasonCode( ) );
    }
  }
 
  // begin local transaction
  if ( ! mgr.begin( ) ) {
    printf(
     "ImqQueueManager::begin ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
    return( (int)mgr.reasonCode( ) );
  }
 
  // get the message
  if ( ! queue.get( msg, getoption ) ) {
    printf( "ImqQueue::get ended with reason code %d\n",
                                (int)queue.reasonCode( ) );
  }
 
  // commit local transaction
  if ( queue.reasonCode( ) == 0 ) {
    if ( ! mgr.commit( ) ) {
      printf(
     "ImqQueueManager::commit ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
      return( (int)mgr.reasonCode( ) );
    }
  } else {
    if ( ! mgr.backout( ) ) {
      printf(
    "ImqQueueManager::backout ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
      return( (int)mgr.reasonCode( ) );
    }
  }
 
  // close the queue
  if ( ! queue.close( ) ) {
    printf( "ImqQueue::close ended with reason code %d\n",
                                (int)queue.reasonCode( ) );
  }
 
  // disconnect from the queue manager
  if ( ! mgr.disconnect( ) ) {
    printf(
 "ImqQueueManager::disconnect ended with reason code %d\n",
                                  (int)mgr.reasonCode( ) );
  }
  return (0);
}