Esper demo created in eclipse

Source: Internet
Author: User

Sper is a Java-developed event stream processing (ESP: event stream processing) and complex event processing (CEP: complex event processing) engine. CEP: a technology that processes real-time events and mines complex patterns from a large number of event data streams. ESP: a technology that filters, analyzes, and obtains meaningful information from a large number of event data streams in real time. This engine can be used in network intrusion detection, SLA monitoring, RFID reading, air transportation control, financial (risk management, fraud detection) and other fields.

On the official website of EsperQuick StartThere is a simple example, but it does not explain how to create this project in eclipse. At the same time, we found that examples in Quick Start had a slight problem in the new version of esper-4.9.0. The following describes how to create an esper_demo project and give a simple explanation.

1. Create a project to import dependency packages

Create a Java project in eclipse, and then import 5 necessary dependent jar files:Esper-4.9.0.jar,Antlr-runtime-3.2.jar, cglib-nodep-2.2.jar, commons-logging-1.1.1.jar, log4j-1.2.16.jar, the several dependent packages are downloaded from the official website.

II,CodeWrite

1. Create an event class

Based on the example on the official website, create a package in SRC:Org. MyApp. Event, and then create an orderevent class under the package.

 1   Package  Org. MyApp. event;  2   3   Public   Class  Orderevent {  4    Private  String itemname;  5     Private   Double  Price;  6   7     Public Orderevent (string itemname, Double  Price ){  8       This . Itemname = Itemname;  9       This . Price = Price;  10   }  11   12     Public  String getitemname (){  13       Return  Itemname;  14   }  15   16     Public   Double  Getprice (){ 17       Return  Price;  18   }  19 }

2. Create a mylistener class and add a listner

 

 1   Package  Org. MyApp. event;  2   3   Import  Com. espertech. Esper. Client. eventbean;  4  Import  Com. espertech. Esper. Client. updatelistener;  5   6   Public   Class Mylistener Implements  Updatelistener {  7     Public   Void  Update (eventbean [] newevents, eventbean [] oldevents ){  8 Eventbean event = newevents [0 ]; 9 System. Out. println ("AVG =" + event. Get ("AVG (price )" ));  10   }  11 }

 

3. Create a main class

 1   Package  Org. MyApp. event;  2   3   Import  Com. espertech. Esper. Client. configuration;  4  Import  Com. espertech. Esper. Client. epserviceprovider;  5   Import  Com. espertech. Esper. Client. epserviceprovidermanager;  6   Import  Com. espertech. Esper. Client. epstatement;  7   8   Public   Class  Test {  9   10    Public   Static   Void  Main (string [] ARGs ){  11   12       //  Epserviceprovider epservice = epserviceprovidermanager. getdefaprovider provider ();  13       //  String expression = "select AVG (price) from org. MyApp. event. orderevent. Win: Time (30 sec )";  14       // Epstatement Statement = epservice. getepadministrator (). createepl (expression );  15   16 Configuration Config = New  Configuration ();  17 Config. addeventtypeautoname ("org. MyApp. Event" );  18 Epserviceprovider epservice = Epserviceprovidermanager. getdefaultprovider (config );  19   20 String EPL = "select AVG (price) from orderevent. Win: Time (30 sec )";  21 Epstatement Statement = Epservice. getepadministrator (). createepl (EPL );  22   23 Mylistener listener = New  Mylistener ();  24   Statement. addlistener (listener );  25   26 Orderevent event = New Orderevent ("shirt", 74.50 );  27  Epservice. getepruntime (). sendevent (event );  28   29   }  30   31 }

Lines 17 should beConfig. addeventtypeautoalias ("org. MyApp. Event ");However, an error is displayed in eclipse. After checking the configuration API, this function is not found. Therefore, it is replaced with addeventtypeautoname, which imports all classes in the package as events ). If epserviceprovidermanager. getdefaultprovider (config) has the config parameter, the default epserviceprovider is returned (which is surprising in the API ).

In this way, you can run it directly. AVG = 74.5. This operation result may be confusing. Replace 26-27 with the following code and try again.

1For(IntI = 0; I <10; I ++){2Epservice. getepruntime (). sendevent (NewOrderevent ("name" + I, (I + 1) * 10));3//Thread. Sleep (1000 );4}

Check the output result. Whether it is clear at a glance.

 

 

 

Related Article

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.