Wednesday, January 28, 2009

Event Driven Architecture:The Observer Pattern

An Event Driven Architecture (EDA) is an extremely loosely-coupled and high-distributed architecture that is best used for asynchronous flows of information. When thinking in terms of service-oriented architectures, we have loosely coupled services that are composed to perform synchronous work in an application or business domain. When this include event handling, then we have a combination of SOA-EDA that can have service invocations that are both synchronous and asynchronous. My interest here is to develop the idea of EDAs.

Basics

An event can be considered any notable change that occurs in or out of a business domain. It might suggest a problem, an opportunity, or threshold. Thus, we need to have a definition and instance of the event. Also, we should have an event header that describes, for example, the following items:

-Event Specification ID
-TypeName
-TimeStamp
-Occurence Number
-Creator

The event body is needed to describe what happened and should be developed by use of an ontology. There are three styles associated with EDA:

(1) Simple Event Processing - A notable event happens that initiates down-stream processing
(2) Stream Event Processing - Both ordinary and important events occur. Here ordinary events are filtered for importance and streamed to subscribers.
(3) Complex Event Processing - Here the evaluation of many different types of events occur.

Because of different event types, there is needed event correlation which can be causal,temporal and/or spatial. Thus, there is a need for event interpreters, event pattern definition and matching, and correlation techniques.

Event Processing

There are four logical layers in our EDA:

(1) Event Generators - The source generates an event.
(2) Event Channels - The channel is the messaging backbone for events.
(3) Event Processing - Events are evaluated against event processing rules. This is the event engine.
(4) Event Driven Activity - The invocation of an activity. For example, the invocation of an activity is pushed by the Event Processing or pulled by subscribers of event publications.

We can implement all four logical layers in the C#.NET language.

C#.NET Language

In a .NET language, an event can be considered to be the outcome of an action. There is an event source and receiver. The object that raises the event is the source and the object that responds is the reciever. A delegate is the communication channel between the event source and receiver. This delegate needs to be declared, instantiated and invocated. For example,

A: Declaration

public delegate int TestDelegate(object obj1, object obj2) ;

B: Instantiation

public TestMethod()
{
TestDelegate TD = new TestDelegate(TestDelegateMethod) ;
}

C: Invocation

TestDelegateMethod(" This is a Test.");

Consider now tying this delegate to an event. Back to the idea of event source and receiver. Since delegates can be called anonymously, we need an event so that a client class can pass in delegates to do something when something happens. For example,

public delegate void TestDelegate(int a1);
public event TestDelegate TestEvent;

Now that we have declared both the delegate and event, we can specify an event handler using the += operator for the attachment. For example,

TestClass.TestEvent += new MyEvent(TestMethod);

we can detach the event by

TestClass.TestEvent -= new MyEvent(TestMethod);

In order to raise the event, we can just make a call to TestEvent(8). Of course,we can extend this to different type of delegates: single cast and multi-cast. The single cast calls only one function, while the multi-cast points to a linked list and calls all the functions as part of a linked list. The namespace for the mulit-cast delegate is System.Multicastdelegate. I think it is instructive here for EDA to look at an example,

public static Delegate Combine (Delegate a, Delegate b) ;

class TestDelegate1{
public void displayDelegate1( string s1)
{
Console.WriteLine("TestDelegate1");
}
}class TestDelegate2
{
public void displayDelegate2( string s1)
{
Console.WriteLine("TestDelegate2");
}
}
public delegate void OnMessage(string s);

class TestMulticastDelegates
{
public static void Main (string[] args)
{
TestDelegate1 md1 = new TestDelegate1();
TestDelegate2 md2 = new TestDelegate2();
OnMessage oM1 = new OnMessage(md1.displayDelegate1);
OnMessage oM2 = new OnMessage(md2.displayDelegate2);
OnMessage omc;
//Combine the delegates and omc points to the linked list
omc=(OnMessage)Delegate.Combine(om1, om2);
Delegate [] arrayOnMessage;
//Array of delegate references
arrayOnMessage = omc.GetInvocationList();
OnMessage om3;
//Navigate through the array to call each delegate
for (int i=0; i < arrayOnMessage.Length; i++)
{
om3 = (OnMessage)arrayOnMessage[i];
om3("string");
}
}
}
One can see how delegates and event handling fit into the framework of the EDA. The delegates are the event channel and their signatures form the event headers and body. For an example of event processing and event driven behavior consider using the Observer pattern.

Observer Pattern in C#.NET

Here is a classic example of the publish/subscribe or observer pattern for delegates and event handling.

using System;
// (2) Event Channel
public delegate void EventHandler(object sender, EventArgs e);
public class Button
{
// (3) Event Processing Engine with rules
protected virtual void OnClicked(EventArgs e)
{ if (Clicked != null)
//(1) This is the event generator
Clicked(this, e);
}
}

public class TestWindow
{
private Button TestButton;
public TestWindow()
{
TestButton = new Button();
//Attach the event to the control for communication
TestButton.Clicked += new EventHandler(TestButton_Clicked);
}
private void TestButton_Clicked(object sender, EventArgs e)
{
//(4) Event Driven Activity
//Method is invoked when Clicked(this,e) is called.
}
}

Conclusion


Using an EDA with event streaming for even data management provides opportunties for simulation, analysis, root-cause analysis, validation, auditing and compliance. By using C#.NET, delegates and events in the observer pattern, it becomes easy to develop an event driven architecture as discussed above.

No comments: