/* * Based on the WebSphere MQ sample program AMQSXAG0.C * * Build with the following: * * cc -I/opt/mqm/inc -I/usr/local/easysoft/unixODBC/include xa_support_sample.c \ * -o xa_support_sample -L/opt/mqm/lib -lmqm \ * -L/usr/local/easysoft/unixODBC/lib -lodbc */ #include #include #include #include #include #include #include /* MQI */ #ifndef OK #define OK 0 /* define OK as zero */ #endif #ifndef NOT_OK #define NOT_OK 1 /* define NOT_OK as one */ #endif #ifndef BOOL #define BOOL MQULONG #endif #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif int main(int argc, char *argv[]) /* ----- START OF MAIN ----- */ /*****************************/ { char QMName[50]=""; /* default QM name */ MQHCONN hCon; /* handle to connection */ MQLONG compCode; /* completion code */ MQLONG connReason; /* MQCONN reason code */ MQOD od = {MQOD_DEFAULT}; /* object descriptor */ MQMD md = {MQMD_DEFAULT}; /* message descriptor */ MQGMO gmo = {MQGMO_DEFAULT}; /* get message options */ MQBO bo = {MQBO_DEFAULT}; /* begin options */ MQLONG options; /* options */ MQHOBJ hObj; /* handle to object */ MQLONG openCompCode; /* MQOPEN completion code */ MQLONG reason; /* reason code */ MQLONG rc=OK; /* return code */ MQLONG msgBufLen; /* message buffer length */ char msgBuf[100]; /* message buffer */ MQLONG msgLen; /* message length received */ int sql_error; /* * ODBC vars */ SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; SQLRETURN ret; /***************************************************************************/ /* First check we have been given correct arguments */ /***************************************************************************/ if (argc != 2 && argc != 3) { printf("Input is: %s 'queue name' 'queue manager name'.\n" "Note the queue manager name is optional\n", argv[0]); exit(-1); } /* endif */ if (argc == 3) /* use the queue manager */ { strcpy(QMName, argv[2]); /* name supplied */ } /***************************************************************************/ /* Connect to queue manager */ /***************************************************************************/ MQCONN(QMName, &hCon, &compCode, &connReason); if (compCode == MQCC_FAILED) { printf("MQCONN ended with reason code %d\n", connReason); exit(-1); /* no point in going on */ } /* endif */ /***************************************************************************/ /* Use input parameter as the name of the target queue */ /***************************************************************************/ strncpy(od.ObjectName, argv[1], (size_t) MQ_Q_NAME_LENGTH); printf("Target queue is %s\n", od.ObjectName); /***************************************************************************/ /* Open the target message queue for input */ /***************************************************************************/ options = MQOO_INPUT_AS_Q_DEF + MQOO_FAIL_IF_QUIESCING; MQOPEN(hCon, &od, options, &hObj, &openCompCode, &reason); if (reason != MQRC_NONE) { printf("MQOPEN ended with reason code %d\n", reason); exit( -1 ); } if (openCompCode == MQCC_FAILED) { printf("Unable to open queue for input\n"); exit( -1 ); } /* endif */ /***************************************************************************/ /* Set up some things for the MQGET */ /***************************************************************************/ msgBufLen = sizeof(msgBuf) - 1; gmo.Version = MQGMO_VERSION_2; /* Avoid need to reset message ID and */ gmo.MatchOptions = MQMO_NONE; /* correlation ID after every MQGET */ gmo.Options = MQGMO_WAIT + MQGMO_CONVERT + MQGMO_SYNCPOINT; gmo.WaitInterval = 15000; /* 15 sec limit for waiting */ /************************************************************************/ /* Start a unit of work */ /************************************************************************/ MQBEGIN (hCon, &bo, &compCode, &reason); if (reason == MQRC_NONE) { printf("Unit of work started\n"); } else { if (reason == MQRC_NO_EXTERNAL_PARTICIPANTS) printf("No participating resource managers registered\n"); else if (reason == MQRC_PARTICIPANT_NOT_AVAILABLE) printf("Participating resource manager not avaliable\n"); else printf("MQBEGIN ended with reason code %d\n" "Unable to start a unit of work\n", reason); /*********************************************************************/ /* If we get a reason code and only a warning on the compCode, there */ /* is something wrong with one or more of the resource managers so */ /* stop looping and sort it out, whatever the compCode. */ /*********************************************************************/ exit( -1 ); } /* endif */ /************************************************************************/ /* Get message off queue */ /************************************************************************/ /* * Start ODBC */ ret = SQLAllocEnv( &henv ); ret = SQLAllocConnect( henv, &hdbc ); ret = SQLConnect( hdbc, "ORACLE-XA", SQL_NTS, NULL, 0, NULL, 0 ); ret = SQLSetConnectAttr( hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0 ); ret = SQLAllocStmt( hdbc, &hstmt ); sql_error = 0; do { MQGET(hCon, hObj, &md, &gmo, msgBufLen, msgBuf, &msgLen, &compCode, &reason); if (reason != MQRC_NONE) { if (reason == MQRC_NO_MSG_AVAILABLE) { printf("No more messages\n"); } else { printf("MQGET ended with reason code %d\n", reason); exit( -1 ); } } else { msgBuf[ msgLen ] = '\0'; printf( "MSG: %s\n", msgBuf ); ret = SQLExecDirect( hstmt, msgBuf, msgLen ); if ( !SQL_SUCCEEDED( ret )) { sql_error = 1; } } } while( reason == MQRC_NONE ); /* * Finish ODBC */ ret = SQLFreeStmt( hstmt, SQL_DROP ); ret = SQLDisconnect( hdbc ); ret = SQLFreeConnect( hdbc ); ret = SQLFreeEnv( henv ); /************************************************************************/ /* Commit, or rollback work */ /************************************************************************/ if ( sql_error == 0 ) { printf( "Commiting work\n" ); MQCMIT(hCon, &compCode, &reason); if (reason == MQRC_NONE) { printf("Unit of work successfully completed\n"); } else { printf("MQCMIT ended with reason code %d completion code " "%d\n", reason, compCode); exit( -1 ); } /* endif */ } /************************************************************************/ /* Close Connection */ /************************************************************************/ MQCLOSE(hCon, &hObj, 0, &compCode, &reason); if (reason != MQRC_NONE) { printf("MQCLOSE ended with reason code %d\n", reason); } /************************************************************************/ /* Disconnect */ /************************************************************************/ MQDISC(&hCon, &compCode, &reason); if (reason != MQRC_NONE) { printf("MQDISC ended with reason code %d\n", reason); } }