先确定理解了Esper的思想下:
**
* Chapter 1. Technology Overview***** 3.cep提供了两种机制处理事件:事件模式和事件流查询。
根据官方网站给的入门例子:
主代码:
package com.doctor.esper.tutorial;
import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
/**
* code for
*
* @see http://www.espertech.com/esper/quickstart.php
*
* @author doctor
*
* @time 2015年5月28日 下午3:51:18
*/
public class QuickStart {
public static void main(String[] args) {
// Configuration
//
// Esper runs out of the box and no configuration is required. However configuration can help make statements more readable and provides the
// opportunity to plug-in extensions and to configure relational database access.
//
// One useful configuration item specifies Java package names from which to take event classes.
//
// This snippet of using the configuration API makes the Java package of the OrderEvent class known to an engine instance:
// In order to query the OrderEvent events, we can now remove the package name from the statement:see line40
Configuration configuration = new Configuration();
configuration.addEventTypeAutoName("com.doctor.esper.tutorial");
// Creating a Statement
// A statement is a continuous query registered with an Esper engine instance that provides results to listeners as new data arrives, in
// real-time, or by demand via the iterator (pull) API.
// The next code snippet obtains an engine instance and registers a continuous query. The query returns the average price over all OrderEvent
// events that arrived in the last 30 seconds:
EPServiceProvider epServiceProvider = EPServiceProviderManager.getDefaultProvider(configuration);
String expression = "select avg(price) from OrderEvent.win:time(30 sec)";
EPStatement epStatement = epServiceProvider.getEPAdministrator().createEPL(expression);
// By attaching the listener to the statement the engine provides the statement's results to the listener:
MyListener myListener = new MyListener();
epStatement.addListener(myListener);
// Sending events
// The runtime API accepts events for processing. As a statement's results change, the engine indicates the new results to listeners right
// when the events are processed by the engine.
// Sending events is straightforward as well:
OrderEvent orderEvent = new OrderEvent("shirt", 75.50D);
epServiceProvider.getEPRuntime().sendEvent(orderEvent);
}
}
定义事件(java普通类形式):
package com.doctor.esper.tutorial;
import com.alibaba.fastjson.JSON;
/**
* Creating a Java Event Class
*
* Java classes are a good choice for representing events, however Map-based or XML event representations can also be good choices depending on
* your
* architectural requirements.
*
* A sample Java class that represents an order event is shown below. A simple plain-old Java class that provides getter-methods for access to
* event
* properties works best:
*
* @author doctor
*
* @time 2015年5月28日 下午3:59:02
*/
public class OrderEvent {
private String itemName;
private double price;
public OrderEvent(String itemName, double price) {
this.itemName = itemName;
this.price = price;
}
public String getItemName() {
return itemName;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
/**
* Adding a Listener
*
* Listeners are invoked by the engine in response to one or more events that change a statement's result set. Listeners implement the UpdateListener
* interface and act on EventBean instances as the next code snippet outlines
*
* @author doctor
*
* @time 2015年5月28日 下午4:02:37
*/
public class MyListener implements UpdateListener {
@Override
public void update(EventBean[] newEvents, EventBean[] oldEvents) {
EventBean eventBean = newEvents[0];
System.out.println("avg = " + eventBean.get("avg(price)"));
}
}06-20 16:03:27.702 main INFO c.e.e.c.s.EPServiceProviderImpl - Initializing engine URI ‘default‘ version 5.2.0
avg = 75.5
原文:http://blog.csdn.net/doctor_who2004/article/details/46574015