Use specific examples to explain how to use the Esper

Source: Internet
Author: User
Tags event listener

This article uses a popular concrete example to explain esper. There are two versions of code links at the end of the article. With this article, you'll learn how to use Esper to learn how to integrate Esper into the spring framework, and learn how to use Apache Active MQ and JMS to provide event data for Esper.

The following is a very simple example of event flow processing (using the Esper engine). On the GitHub can see a complete code, ~ link at the end of the article ~. What is a complex event processing (CEP).

Complex event Processing (CEP) or Event flow processing (ESP) is a commonly used technique in event-driven systems. These types of systems respond to event data streams in real time. Often used in financial transactions, fraud identification, and process monitoring-you need to identify, understand, and respond quickly to patterns in a data event stream. key components of the CEP system

The CEP system is just as reversed as a typical database model. A typical database stores data, runs queries on data, and CEP stores ' query statements, matching queries or filters and matches for real-time streaming data.

To do this basically requires that data-in the form of "events"-is commonly represented as a normal Java class. Query-Use the EPL (' Event processing language ') listener-If a result is returned, it will be triggered to "perform certain actions". (callback function) A simple example-a nuclear power plant

Take the nuclear power plant as an example.

Now, this is just an example-suppose you are most concerned about the critical temperature of a nuclear power plant. Monitoring core temperature

Suppose our power station had a power station, and if it was too hot--very bad things would happen. So it is very important to detect the temperature at the core of the nuclear power plant and send out the warning message under abnormal circumstances.
Here to write a picture description

Suppose we have a thermometer that reads the core temperature per second-and sends the data to the central monitoring system. what are the requirements.

When 3 kinds of events are found, we need to warn:

Monitoring
Just tell us the average temperature every 10 seconds--for reference.

Warning
Warn us if we have 2 consecutive temperatures exceeding a certain threshold

Critical
Remind us that if we have 4 consecutive events, the first event exceeds a certain threshold, and each subsequent event is greater than the last one-the last one is 1.5 times times larger than the first. This means there is a sudden rising temperature spike-a bit like the following figure. Let's assume that this is a very bad thing.
using Esper

We can build systems to handle these requirements in a variety of ways. This article will use Esper to solve this problem.

Esper generally does this: you use Esper to create 3 queries (using the Epl-esper query language) to model these event patterns. We then attach a listener (listener) to each query-this listener is triggered when the EPL detects a match pattern for the event. Create a Esper service, bind the above listener and it, and complete the registration.

When there are matching data, the Esper service sends messages to the associated listener.

our simple Esper solution the core of the system is the 3 queries used to detect events. Query 1-monitor (monitor average temperature only)

Select AVG (value) as Avg_val from 
TemperatureEvent.win:time_batch (sec)
Query 2-Warning (tell us if we have 2 consecutive events that violate thresholds)
SELECT * from Temperatureevent '
 match_recognize ( 
   measures A as Temp1, B as Temp2 pattern 
   (a b) 
   Define
  a as A.temperature >, 
     B as B.temperature > 400)
Query 3-key-4 consecutive rise values over 100, fourth value is 1.5 times times larger than the first
SELECT * FROM Temperatureevent 
match_recognize ( 
measures A as Temp1, B as Temp2, C as Temp3, D as Temp4 
patte RN (a B C D) 
define
A as A.temperature < B as 
(A.temperature < B.value), C as 
(B.temperatur e < C.value), 
D as (C.temperature < D.value) and D.value > (A.value * 1.5))
Some code Snippets temperatureeventAssuming that our incoming data arrives in the form of Temperatureevent Pojo, the Esper message data can be passed through the JMS queue and the listener converts it to Pojo. Using JMS in this way can separate us from the incoming data structure and improve the flexibility of the project. Only normal Java classes are used here. Here is an example of our Pojo, which indicates the type of event.
Package com.cor.cep.event;

Import Java.util.Date; 
 /** * Immutable temperature Event class. 
 * The Process Control system creates the events.
 * The Temperatureeventhandler picks these are up * and processes them.

    * * public class Temperatureevent {/** temperature in celcius. * * private int temperature; /** Time Temerature reading is taken.

    * * Private Date timeofreading;
     /** * Single value constructor.
     * @param value temperature in Celsius.
     * */** * Temerature constructor. * @param temperature temperature in Celsius * @param timeofreading time of Reading/public Temperatureeven
        T (int temperature, Date timeofreading) {this.temperature = temperature;
    this.timeofreading = timeofreading;
     }/** * Get the temperature.
    * @return Temperature in Celsius */public int gettemperature () {return temperature; }/** * Get time temperature reading WAS taken.
    * @return Time of Reading */public Date gettimeofreading () {return timeofreading;
    @Override public String toString () {return "temperatureevent [" + Temperature + "C]";
 }

}
Handle this eventThe primary handler class initializes the Esper service in Temperatureeventhandler.java. 3 Statements were created and a listener was added for each statement
/**
 * Auto initialise Our service after the Spring bean wiring is complete.
 */
@Override public
void Afterpropertiesset () throws Exception {
    initservice ();
}


/**
 * Configure Esper Statement (s).
 */Public
void Initservice () {

    Configuration config = new Configuration ();

    Recognise domain objects in-package in Esper.
    Config.addeventtypeautoname ("Com.cor.cep.event");
    Epservice = epserviceprovidermanager.getdefaultprovider (config);

    Createcriticaltemperaturecheckexpression ();
    Createwarningtemperaturecheckexpression ();
    Createtemperaturemonitorexpression ();
}
Create a critical temperature warning and register the listener.
/**
  * EPL to check for a sudden critical rise across 4 events, 
  * Where's the last event is 1.5x greater than the FIR St. 
  * This is checking to a sudden, sustained escalating 
  * rise in the temperature
  /
 private void createcritical Temperaturecheckexpression () {

     log.debug ("Create Critical temperature Check Expression");
     Epadministrator epadmin = Epservice.getepadministrator ();
     Criticaleventstatement = 
             Epadmin.createepl (criticaleventsubscriber.getstatement ());
     Criticaleventstatement.setsubscriber (Criticaleventsubscriber);
 }
Finally-an example of a key event listener.
Package com.cor.cep.subscriber;

Import Java.util.Map;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;

Import org.springframework.stereotype.Component;

Import com.cor.cep.event.TemperatureEvent; /** * Wraps Esper Statement and Listener.
 No dependency on Esper libraries. * * @Component public class Criticaleventsubscriber implements Statementsubscriber {/** Logger * * private static

    Logger LOG = Loggerfactory.getlogger (Criticaleventsubscriber.class); /** Minimum starting threshold for a critical event.

    * * private static final String critical_event_threshold = "100";
     /** * If The last event in a critical sequence are this much greater * than the first-issue a critical alert.

    * * private static final String Critical_event_multiplier = "1.5";
        /** * {@inheritDoc} */Public String getstatement () {//Example using ' Match recognise ' syntax. String criticaleventexpression = "SELECT * FROM T"Emperatureevent "+" match_recognize ("+" measures A as Temp1, B as Temp2, C as Temp3, D As Temp4 "+" pattern (a B C D) "+" define "+" A as A.temperatur
                E > "+ critical_event_threshold +", "+" B as (A.temperature < b.temperature), " + "C as" (B.temperature < C.temperature), "+" D as (C.temperature < D.temperatur  e) "+" and d.temperature > "+" (a.temperature * "+ critical_event_multiplier +") "+

        ")";
    return criticaleventexpression;
     /** * Listener method called when Esper has detected a pattern match. */public void update (map<string, temperatureevent> eventmap) {//1st temperature in the Critical sequ
        ence temperatureevent Temp1 = (temperatureevent) eventmap.get ("Temp1"); 2nd temperature in the Critical Sequence temperatureevent temp2 = (temperatureevent) eventmap.get ("Temp2"); 3rd temperature in the Critical Sequence temperatureevent Temp3 = (temperatureevent) eventmap.
        Get ("Temp3"); 4th temperature in the Critical Sequence temperatureevent temp4 = (temperatureevent) eventmap.

        Get ("Temp4");
        StringBuilder sb = new StringBuilder ();
        Sb.append ("***************************************");
        Sb.append ("n [ALERT]: CRITICAL EVENT detected!");
        Sb.append ("n" + Temp1 + ">" + temp2 + ">" + Temp3 + ">" + temp4);

        Sb.append ("n***************************************");
    Log.debug (Sb.tostring ());
 }


}
Code Run

The complete instructions for running the demo can be found here:

Here are two GitHub code, they are the same in the Esper part, the difference is that the first use of a Pojo class to generate Esper messages, relatively simple. The second was expanded on the basis of the first ground, adding JMS and Apache ActiveMQ. This means using JMS to generate Esper messages. If you want to execute the second code successfully, you will need to install an additional Apache ActiveMQ. Please read the Readme file for each item in detail.

1. Github.com/corsoft/esper-demo-nuclear

2. Https://github.com/liangyihuai/Esper4SpingJMS

An example of running a demo looks like this-it generates random temperature events and sends them through the Esper processor.

When any one of our 3 queries detects a match-the debug information is displayed to the console. In a real-world solution, each of these three listeners handles these events in a different way-perhaps by sending a message to alert the queue/endpoint to get the other parts of the system to get the processing.
Conclusions

Using a system like Esper is an easy way to monitor and identify data patterns in real time with minimal code.

This article is just a simple use case. See the Esper Web site for more information.

This article is translated from: Complex-event-processing-made

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.