MQIのサンプルコーディング(C言語)

C言語でのコーディング例を示します。

/* All Rights Reserved, Copyright (C) 2003, Hitachi, Ltd. */
/***********************************************************
**  mqcsample.c
**  functions = main()
***********************************************************/

/***********************************************************
**  name = main()
**  func = main function of UAP
**      (1)start UAP(output start message)
**      (2)MQCONN(Connect queue manager)
**      (3)MQOPEN(Open queue)
**      (4)MQBEGIN(begin local transaction)
**      (5)MQPUT(Put message)
**      (6)MQBACK(rollback local transaction)
**      (7)MQCMIT(commit local transaction)
**      (8)MQBEGIN(begin local transaction)
**      (9)MQGET(Get message)
**      (10)MQBACK(rollback local transaction)
**      (11)MQCMIT(commit local transaction)
**      (12)MQCLOSE(Close queue)
**      (13)MQDISC(Disconnect queue manager)
**      (14)finish UAP(output end message)
***********************************************************/
#include    <stdio.h>
#include    <string.h>
#include    <cmqc.h>

#define PUT_DATA     "******** sample put data ********"

int main(void)
{
   MQCHAR48    QueueManager = " ";      /* queue manager name  */
   MQLONG      comp_code,               /* completion code     */
               reason;                  /* reason code         */
   MQHCONN     qm_handle;               /* connection handle   */
   MQCHAR      message_data[200];       /* buffer              */
   MQHOBJ      que_handle;              /* object handle       */
   MQLONG      buffer_length;           /* buffer length       */
   MQLONG      data_length;             /* message length      */
   MQLONG      open_options;            /* open options        */
   MQOD        obj_desc = { MQOD_DEFAULT };
                                        /* object descriptor   */
   MQMD        msg_desc = { MQMD_DEFAULT };
                                        /* message descriptor  */
   MQPMO       put_options = { MQPMO_DEFAULT };
                                        /* put message options */
   MQGMO       get_options = { MQGMO_DEFAULT };
                                        /* get message options */
   MQBO        begin_options = { MQBO_DEFAULT };
                                        /* begin options       */

   /* set the queue name */
   strcpy(obj_desc.ObjectName, "dynq1");

   /* set the message data */
   strcpy(message_data, PUT_DATA) ;
   buffer_length = strlen(message_data);

   printf("MQTRN:Start service. ¥n");

   /* MQCONN( connect to the queue manager ) */
   MQCONN(QueueManager, &qm_handle, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQCONN. CODE = %ld¥n", reason);
       goto PROG_END;
   }

   /* MQOPEN( open the queue ) */
   open_options = MQOO_OUTPUT | MQOO_INPUT_AS_Q_DEF;
   MQOPEN(qm_handle, &obj_desc, open_options, &que_handle,
           &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQOPEN. CODE = %ld¥n", reason);
       goto MQ_END;
   }

   /* MQBEGIN( begin local transaction ) */
   MQBEGIN(qm_handle, &begin_options, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQBEGIN. CODE = %ld¥n",reason);
       goto MQ_END;
   }

   /* MQPUT( put the message ) */
   put_options.Options = MQPMO_SYNCPOINT;
   MQPUT(qm_handle, que_handle, &msg_desc, &put_options,
           buffer_length, (PMQBYTE)message_data,&comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQPUT. CODE = %ld¥n", reason);
       /* MQBACK( rollback local transaction ) */
       MQBACK(qm_handle, &comp_code, &reason);
       if (comp_code != MQCC_OK)
       {
           printf("MQTRN:Failed at MQBACK. CODE = %ld¥n",reason);
       }
       goto MQ_END;
   }
 
   /* MQCMIT( commit local transaction ) */
   MQCMIT(qm_handle, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQCMIT. CODE = %ld¥n", reason);
       goto MQ_END;
   }

   /* MQBEGIN( begin local transaction ) */
   MQBEGIN(qm_handle, &begin_options, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQBEGIN. CODE = %ld¥n",reason);
       goto MQ_END;
   }

   /* MQGET( get the message ) */
   get_options.Options = MQGMO_SYNCPOINT | MQGMO_NO_WAIT;
   MQGET(qm_handle, que_handle, &msg_desc, &get_options,
           buffer_length, (PMQBYTE)message_data,
           &data_length, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQGET. CODE = %ld¥n", reason);
       /* MQBACK( rollback local transaction ) */
       MQBACK(qm_handle, &comp_code, &reason);
       if (comp_code != MQCC_OK)
       {
           printf("MQTRN:Failed at MQBACK. CODE = %ld¥n",reason);
       }
       goto MQ_END;
   }
 
   /* MQCMIT( commit local transaction ) */
   MQCMIT(qm_handle, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQCMIT. CODE = %ld¥n", reason);
       goto MQ_END;
   }

   /* MQCLOSE( close the queue ) */
   MQCLOSE(qm_handle, &que_handle, MQCO_NONE, &comp_code,
               &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQCLOSE. CODE = %ld¥n",reason);
   }

MQ_END:
   /* MQDISC( disconnect from the queue manager ) */
   MQDISC(&qm_handle, &comp_code, &reason);
   if (comp_code != MQCC_OK)
   {
       printf("MQTRN:Failed at MQDISC. CODE = %ld¥n", reason);
   }

PROG_END:
   printf("MQTRN:Terminate service. ¥n");
   return(0);
}