16.20 - Complete Sample Code (START) - Teradata Ecosystem Manager

Teradata® Ecosystem Manager API Reference

prodname
Teradata Ecosystem Manager
vrm_release
16.20
created_date
December 2020
category
Programming Reference
featnum
B035-3204-107K

The following is a complete sample code used to send multiple START events. It also shows how Ecosystem Manager environment variables are used in API calls.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <tmsmapi.h>
#include <locale.h>

//Start events require UofW
int main (int argc, char** argv)
{
	TM_Context* pCtx = NULL;
	MSM_UofW* pUOW = NULL;
	MSM_WEvent* pStartEvent = NULL;
	wchar_t *szUowId = L"sampleUofWId";
	wchar_t *szUowClass = L"sampleUnitOfWorkClass";
	wchar_t *szTDPID = L"sampleTDPID";
	wchar_t *szJobStep = L"Logon";
	int status = 0;

#ifndef _WIN32
        setlocale(LC_ALL, "");
#endif /* _WIN32 */
	
//initialize Api
	// Do not forget to call TM_Free(pCtx) at the end
	pCtx = TM_Init();
	fwprintf(stderr, L"Initializing TMSM API.\n");
	if (pCtx != NULL)
	{
		 fwprintf(stderr, L"Creating UoW.\n");
		 //Create empty UofW placeholder.
		// Do not forget to call MSM_FreeUOW at the end.
		pUOW=MSM_CreateUOW(pCtx);
		if (pUOW != NULL)
		{
			 //Explicitly assign UofW members
			 pUOW->szUowId=szUowId;//set UofWId
			 pUOW->szUowClass=szUowClass;//set UofWclass
			 fwprintf(stderr, L"Creating Event.\n");
	
		//Create Event placeholder.
	// Do not forget to call MSM_FreeWEvent at the end.
	// Start events require UofW
			 pStartEvent = MSM_CreateWEvent(pCtx,pUOW);
			 if (pStartEvent != NULL)
			 {
				//Simple start
				fwprintf(stderr, L"Sending Start Event.\n");
				//set event type
				pStartEvent->eventType=STARTEVENT;
				//set TDPID
				pStartEvent->szTDPID=szTDPID;
				// set resource id
				pStartEvent->szResourceId=TMSM_WRESOURCETYPE_FLOAD;		
				//Add multi-argument resourceType													 		MSM_AddWResourceType(pCtx,pStartEvent,TMSM_WRESOURCETYPE_FLOAD);	
				//set job step
				pStartEvent->szJobStep = szJobStep;
				//send event
				status=MSM_SendWEvent(pCtx,pStartEvent);				
				fwprintf(stderr, L"SendEvent status %d .\n",status);
				//Free and clear event members for reuse
				MSM_FreeAndClearWEventMembers(pCtx,pStartEvent);
				//Clear UnitOfWork members for resuse
				MSM_ClearUofWMembers(pCtx,pUOW);
				//Update from environment - export variables
				fwprintf(stderr, L"Sending Start Event.\n");
				pStartEvent->eventType=STARTEVENT;
				pStartEvent->szTDPID=szTDPID;
				pStartEvent->szResourceId=TMSM_WRESOURCETYPE_FLOAD;
				//Add multi-argument resourceType
				MSM_AddWResourceType(pCtx,pStartEvent,TMSM_WRESOURCETYPE_FLOAD);					pStartEvent->szJobStep = szJobStep;
				// Update UnitOfWork from environment - export variables
				status=MSM_UpdateUofWFromEnvironment(pCtx,pUOW);							fwprintf(stderr, L"update UOW status from env %d .\n",status);
				
	//Update Event from environment - export variables
				status=MSM_UpdateWEventFromEnvironment(pCtx,pStartEvent);						fwprintf(stderr, L"update event status from env %d .\n",status);
				//send event
				status=MSM_SendWEvent(pCtx,pStartEvent);
				fwprintf(stderr, L"SendEvent status %d .\n",status);
				fwprintf(stderr,L"UnitOfWorkId %ls.\n", pUOW->szUowId);
				//Free event members. Enables pStartEvent resuse
				MSM_FreeWEventMembers(pCtx,pStartEvent);
				//Clear UnitOfWork members for resuse
				MSM_ClearUofWMembers(pCtx,pUOW);
				
	//Explicitly generate unique UnitOfWorkId and read it before 					
				// sending event
				fwprintf(stderr, L"Sending Start Event.\n");
				pStartEvent->eventType=STARTEVENT;
				pStartEvent->szTDPID=szTDPID;
				pStartEvent->szResourceId=TMSM_WRESOURCETYPE_FLOAD;
				MSM_AddWResourceType(pCtx,pStartEvent,TMSM_WRESOURCETYPE_FLOAD);
				pStartEvent->szJobStep = szJobStep;
				//Update Event from environment - export variables
				status=MSM_UpdateWEventFromEnvironment(pCtx,pStartEvent);
				fwprintf(stderr, L"update event status from env %d .\n",status);
				// Generate unique UnitOfWorkId
				status=MSM_UpdateUofWWithUniqueId(pCtx,pUOW);
				// send event
				status=MSM_SendWEvent(pCtx,pStartEvent);
				fwprintf(stderr, L"SendEvent status %d .\n",status);
				fwprintf(stderr,L"Generate unique uowid status %d and unique
							UnitOfWorkId %ls.\n", status,pUOW->szUowId);
				// Enables pStartEvent resuse
				MSM_FreeWEventMembers(pCtx,pStartEvent);
				MSM_ClearUofWMembers(pCtx,pUOW);
				
	//Implicitly generate unique UnitOfWorkId and reda it after sending 
				//event
				fwprintf(stderr, L"Sending Start Event.\n");
				pStartEvent->eventType=STARTEVENT;
				pStartEvent->szTDPID=szTDPID;
				pStartEvent->szResourceId=TMSM_WRESOURCETYPE_FLOAD;
				MSM_AddWResourceType(pCtx,pStartEvent,TMSM_WRESOURCETYPE_FLOAD);
				pStartEvent->szJobStep = szJobStep;
				// Update Event from environment - export variables
				status=MSM_UpdateWEventFromEnvironment(pCtx,pStartEvent);
				fwprintf(stderr, L"update event status from env %d .\n",status);
				// send event
				status=MSM_SendWEvent(pCtx,pStartEvent);
				fwprintf(stderr,L"unique uowid generated implicitly status %d and
					unique UnitOfWorkId %ls.\n", status,pUOW->szUowId);
				fwprintf(stderr, L"SendEvent status %d .\n",status);
				// Enables pStartEvent resuse
				MSM_FreeAndClearWEventMembers(pCtx,pStartEvent);
				MSM_ClearUofWMembers(pCtx,pUOW);
				
			//Free event. Do not forget to call this
				fwprintf(stderr, L"Freeing Event.\n");
				MSM_FreeWEvent(pCtx,pStartEvent);
				
			//Free UnitOfWork. Do not forget to call this.
				fwprintf(stderr, L"Freeing UofW.\n");
				MSM_FreeUOW(pCtx,pUOW);
				
			//Free Context.do not forget to call this.
				fwprintf(stderr, L"Freeing Context.\n");
				TM_Free(pCtx);
			}
			else
			{
				fwprintf(stderr, L"failed to create the event");
			}
		}
		else
		{
			fwprintf(stderr, L"failed to create the unit of work");
		}
	}
	else
	{
		fwprintf(stderr, L"failed to create the context\n");
	}

Complete sample code for sending other event types can be found in $EM_HOME/sdk/samples directory.