The following example shows how to log an event that occurs on the queue. The event that occurs is a put message request.
In this example, a central log is set up for all queues using the queue activate and close rules. This log is then used to keep track of all putMessage operations. Because the log is shared between rules invocations, the information needed to access the log is stored in the rules private data structure. In this case, the private data structure contains a file handle for passing between rules invocations:
struct myRulesData_ { // rules instance structure MQeAdministratorHndl hAdmin; / administrator handle to carry around between // rules functions FILE * ifp; // file handle for logging rules }; typedef struct myRulesData_ myRules;
In the rules queue activate function, the file is opened and the activation of the queue logged:
MQEVOID myRules_activateQueue(MQeRulesActivateQueue_in_ * pInput, MQeRulesActivateQueue_out_ * pOutput) { MQERETURN rc = MQERETURN_OK; MQECHAR * qName; MQEINT32 size; // recover the private data from the input structure parameter pInput myRules * myData = (myRules *)(pInput->pPrivateData); MQeExceptBlock * pExceptBlock = (MQeExceptBlock *)(pOutput->pExceptBlock); SET_EXCEPT_BLOCK_TO_DEFAULT(pExceptBlock); if(myData->ifp == NULL) { // initialized to NULL in the rules initialization function myData->ifp = fopen("traceFile.txt","w"); rc = mqeString_getUtf8(pInput->hQueueName, pExceptBlock, NULL, &size); if(MQERETURN_OK == rc) { qName = malloc(size); rc = mqeString_getUtf8(pInput->hQueueName, pExceptBlock, qName, &size); if(MQERETURN_OK == rc && myData->ifp != NULL) { fprintf(myData->ifp, "Activating queue %s \n", qName); } } } }
In the rules queue close function, the file is closed after the closure of the queue is logged:
MQEVOID myRules_closeQueue(MQeRulesCloseQueue_in_ * pInput, MQeRulesCloseQueue_out_ * pOutput) { MQERETURN rc = MQERETURN_OK; MQECHAR * qName; MQEINT32 size; // recover the private data from the input structure parameter pInput myRules * myData = (myRules *)(pInput->pPrivateData); MQeExceptBlock * pExceptBlock = (MQeExceptBlock *)(pOutput->pExceptBlock); SET_EXCEPT_BLOCK_TO_DEFAULT(pExceptBlock); if(myData->ifp != NULL) { rc = mqeString_getUtf8(pInput->hQueueName, pExceptBlock, NULL, &size); if(MQERETURN_OK == rc) { qName = malloc(size); rc = mqeString_getUtf8(pInput->hQueueName, pExceptBlock, qName, &size); if(MQERETURN_OK == rc) { fprintf(myData->ifp, "Closing queue %s \n", qName); } } fclose(myData->ifp); MyData->ifp = NULL; } }
The rules put message function ensures that each put message operation is logged:
MQERETURN myRules_putMessage(MQeRulesPutMessage_in_ * pInput, MQeRulesPutMessage_out_ * pOutput) { MQERETURN rc = MQERETURN_OK; MQECHAR * qName, * qMgrName; MQEINT32 size; // recover the private data from the input structure parameter pInput myRules * myData = (myRules *)(pInput->pPrivateData); MQeExceptBlock * pExceptBlock = (MQeExceptBlock *)(pOutput->pExceptBlock); SET_EXCEPT_BLOCK_TO_DEFAULT(pExceptBlock); if(myData->ifp != NULL) { rc = mqeString_getUtf8(pInput->hQueueName, pExceptBlock, NULL, &size); if(MQERETURN_OK == rc) { qName = malloc(size); rc = mqeString_getUtf8(pInput->hQueueName, pExceptBlock, qName,&size); } if(MQERETURN_OK == rc) { rc = mqeString_getUtf8(pInput->hQueue_QueueManagerName, pExceptBlock, NULL, &size); if(MQERETURN_OK == rc) { qMgrName = malloc(size); rc = mqeString_getUtf8(pInput->hQueue_QueueManagerName, pExceptBlock, qMgrName, &size); } } if(MQERETURN_OK == rc) { fprintf(myData->ifp, "Putting a message onto queue %s on queue manager %s\n",qName, qMgrName); } } /* allow the operation to proceed regardless of what went wrong in this rule */ SET_EXCEPT_BLOCK_TO_DEFAULT(pExceptBlock); return EC(pExceptBlock); }