Complex Event Processing / Java / Oracle

Complex Event Processing with Esper–Tutorial For Beginners

Introduction

This is a tutorial for beginners who are interested in CEP (Complex Event Processing) using Esper. We will build a fictional real-time surveillance system which alerts on conditions we will define. You will learn how to use Esper to process real-time events and you may also learn a thing or two about sockets in Java.

 

The Story

Boys at the station have been tipped off regarding some illegal activities at The Genco Pura Olive Oil Company. They suspect that some illegal cargo is about to move through the company. In order to bust the gig they have come out with a plan to monitor phone calls to and from certain phone numbers. They have identified phone numbers used by Vito and Tom and they’d like to know when they make a call or they receive a call. The scenario can be expanded to record their conversations but that would require a court’s approval and it’s just too much work for a Esper tutorial.

 

Tools of Trade

This tutorial is about Esper which should be downloaded and extracted to an accessible location.

Language of choice is Java so it’s Esper for Java we are after.

Phone numbers are fictitious and taken from this site. http://www.acma.gov.au/WEB/STANDARD..PC/pc=PC_2330

Vito’s number is: 1800 160 401. Yes he has a 1800 number. He does not mind paying when other people call him.

Tom’s number is: 0491 570 110

IDE: I use Netbeans but it doesn’t really matter. Any IDE or text editor of choice will do.

 

Design

Our bust the bad guys solution relies on a server component which sends messages about calls being made. These messages are received by a client application which feeds them into Esper engine. Esper then looks at these messages and based on instructions given to it, alerts the user. Alert mechanism is simply displaying attention grabbing text on console. This is a CEP and Esper tutorial remember? Do not get carried away.

Below is a pictorial representation of the design.

image

 

The Message

The server sends a message to the client whenever a call is made. This message is then received by client. We can express our message as a POJO which are supported by Esper. The message object maintains information about from and to phone numbers, time of call and duration of call. For this tutorial we are only interested in the phone numbers.

Server

The server application accepts socket connections on a port and then starts sending messages to the connected client. It maintains a list of phone numbers and simulates call data records also known as CDR in telco lingo.  This is a stock standard sockets server which has nothing to do with Esper.

Client

Client application is where interesting stuff happens. By establishing a socket connection it receives a message for each call from server.

You can see in the code above that an instance of CallDataRecord class is created from data in ObjectInputStream. A call is made to sendEvent method of EPRuntime object and retrieved CallDataRecord is sent as an argument. How is this data handled by Esper? We have two classes which are Esper listeners and they are ready to act when invoked. We have one listener for calls made “from” and another for calls made “to”. For a class to act as a listener in Esper it must implement UpdateListener interface.

So what invokes a particular listener? This is handled by Esper when we register a listener to a particular EPL statement. An EPL statement is like a SQL query with the familiar Select * from syntax. This is our EPL statement for “from” calls. Note that after creating an instance of EPStatement class we register the appropriate listener.

For it to all work there is a little more setup required. Let’s go through it. First of all we need a Configuration object to which we will add an event type. Note that in our EPL statements we are querying from CDR. This CDR is defined as a Event Type in the configuration object.

Next thing we need is a EPRuntime which is the Esper runtime object.

Finally we run our client and pass it an instance of EPRuntime so that our client can send events to Esper engine.

Here is the full code for Client application.

For the client to build we need to reference following libraries.

Esper Libraries

These jars can be found in the root esper folder and root esper/esper/lib folders.

 

Output

After running Server and Client for few seconds we can see that Esper is notifying us of calls made to or from Vito or Tom’s mobile. Watch out guys.

Esper Output

 

Conclusion

In this tutorial we saw how simple it is to create an application which can process events in real-time. Real power of Esper is the raw speed by which it can do complex real-time processing. Esper is also simple to understand and work with. There is a lot more to Esper than this simple tutorial. Maybe I will write a bit more in future posts.

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">