How To Build & Configure A Custom Event Handler

Learn to build and configure a custom event handler

Last published at: July 30th, 2024

FlowWright Enterprise Service Bus (ESB

FlowWright has a powerful ESB similar to a traditional publisher-subscriber model, but one that improves that model to improve performance.  FlowWright utilizes Event definitions 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, and the same Event handler can also be 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 it. 

 

Creating a new Event handler 

The below video shows how to create a custom event handler for FlowWright v10.

 

 

Here's the code that was used in the above example:

using FlowWright.API;
using FlowWright.API.ESB;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FW10CustomItems
{
    public class FWTestHandler : IFWEventHandler
    {
        public void Execute(FWEventContext oContext)
        {
            try
            {
                Hashtable oEventParms = oContext.GetEventParameters();
 
                oEventParms["eventID"] = oContext.EventID;
 
                FWProps oProps = oContext.GetProperties();
 
                oEventParms["eventDefID"] = (oProps["EVENTDEFID"]).ToString();
 
                oEventParms["createdOn"] = oProps["EVENTCREATEDATETIME"].ToString();
 
                string eventName = oContext.ESB.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);
            }
        }
    }
}