Building and configuring a custom event handler

Last published at: May 2nd, 2023

FlowWright Enterprise Service Bus (ESB) 

FlowWright has a powerful ESB that is similar to a traditional publisher-subscriber model, but a one that improves that model to improve performance.  FlowWright utilizes Event definition and Event handler definitions to create a bridge between Events and Event handlers. 

 

Once Events and Event handlers are defined within FlowWright using the Configuration Manager, Event handlers can be assigned to Events.  A single Event may have any number of Event handlers assigned to that Event.  The same Event handler can be also attached to multiple events. 

Events can be published using the .Net API or the Web service API. 

 

Once an Event is published to the ESB, assigned event handlers will act on the Event. 

Creating a new Event handler 

A new event handler can be implemented using the interface deEventHandler. The interface has one simple method called “execute” that needs to be implemented.  Below is code for a sample event handler: 

public class clsWriteTextFileHandler : deIEventHandler

      {

            /// <summary>

            /// Executes the specified o context.

            /// </summary>

            /// <param name="oContext">The o context.</param>

            public void execute(deEventContext oContext)

            {

                  try

                  {

                        Hashtable oEventParms = oContext.getEventParameters();

 

                        oEventParms["eventID"] = oContext.eventID;

 

                clsProps oProps = oContext.getProperties();

                        oEventParms["eventDefID"] = (oProps["EVENTDEFID"]).ToString();

                        oEventParms["createdOn"] = oProps["EVENTCREATEDATETIME"].ToString();

 

                        string eventName = oContext.oESB.getEventDefinitionName(oProps["EVENTDEFID"].ToString());

                        oEventParms["eventName"] = eventName;

                        oEventParms["executedOn"] = DateTime.Now.ToString();

 

                        if (!Directory.Exists(@"c:\temp\EventData"))

                        {

                              Directory.CreateDirectory(@"c:\temp\EventData");

                        }

 

                        string sFormatEventName = eventName.Replace(" ", "");

                        string sFilePath = Path.Combine(@"c:\temp\EventData\", oContext.eventID + "-" + sFormatEventName + ".txt");

 

                        if (File.Exists(sFilePath))

                        {

                              oContext.markEventProcessed(eventStatus.executed);

                              return;

                        }

 

                        ArrayList oList = new ArrayList(oEventParms.Keys);

                        oList.Sort();

 

                        foreach (string sItem in oList)

                        {

                              File.AppendAllText(sFilePath, $"{sItem} - {oEventParms[sItem].ToString()}{Environment.NewLine}");

                        }

 

                        oContext.markEventProcessed(eventStatus.executed);

                  }

                  catch (Exception ex)

                  {

                        oContext.markEventProcessed(eventStatus.error, ex.Message);

                  }

            }

      }


The above code writes all parameters of the event to a text file, it’s a simple event handler that demonstrates the configuration and execution of the event.  After compiling the event handler to a DLL, place the DLL file within the “BIN” directory: 

C:\inetpub\wwwroot\cDevWorkflow\bin 

Using the menu for the ESB, select menu item “Event Handlers”: 

 

Use the “Auto Detect” feature to auto configure the event handler within FlowWright: 

 

 Select the new event handler from the list and select “Manage->Configure” menu item. 


Using the new event handler with an event

Let’s create a new event within FlowWright by navigating to “Event Definitions” on the menu: 

 

Create a new event called “sampleEvent” by selecting “Actions->Create” dialog: 

 

 

  

Once the event definition is created, let’s join the event handler to the event definition, navigate to the “ESB” menu item: 

 

Select the menu item “Actions->Create” to create a new subscriber for the event: 

 

 

Configure the subscriber using the values/selection shown in the above create dialog. 

 

Testing the event

Now that the event, event handler and the subscriber is configured, let’s test the event using the “Generate Events” UI: 

Select the event “sampleEvent” and click the button “Generate Event” to publish the event to the ESB. 

In order to view the generated event, navigate to the “View Events” UI: 

 

You should see the newly generated event in the list. 

 

Publishing events to the ESB 

The following code shows how to publish an Event to the FlowWright ESB.   

 

//create the Event service bus object

         deEventServiceBus oEvt = new deEventServiceBus(sConnStr, "admin");

 

         //get the ID of the event definition

         string sEventID = oEvt.getEventDefinitionID("sampleEvent");

 

         //create a list of event parameters to pass in

         Hashtable oInParms = new Hashtable();

         oInParms["data1"] = "Data Elements";

         oInParms["verified"] = "yes";

 

         //publish the event

         bool bFlag = oEvt.publishEvent(sEventID, "TestApp", oInParms, eventPriority.high);