esper官网 可下载jar包和documentation http://www.espertech.com/esper。
eclipse 新建mawen项目test。
在 pom.xml文件中添加如下依赖:
<dependencies>
<dependency>
<groupId>com.espertech</groupId>
<artifactId>esper-common</artifactId>
<version> 8.1.0 </version>
</dependency>
<dependency>
<groupId>com.espertech</groupId>
<artifactId>esper-compiler</artifactId>
<version> 8.1.0 </version>
</dependency>
<dependency>
<groupId>com.espertech</groupId>
<artifactId>esper-runtime</artifactId>
<version> 8.1.0 </version>
</dependency>
</dependencies>
选中test project,右键Propeties->Mawen->Update Project 确定更新依赖,自动下载依赖包。
创建事件类TickData
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class TickData {
String symbol;
double askPrice;
double bidPrice;
String time;
public String getSymbol(){
return symbol;
}
public double getAskPrice(){
return askPrice;
}
public double getBidPrice(){
return bidPrice;
}
public String getTime() {
return time;
}
public TickData() {
String [] symbolArray = {"A","B","C"};
long seed = System.currentTimeMillis();
Random rint = new Random(seed);
int i = rint.nextInt(3);
this.symbol = symbolArray[i];
this.askPrice = 10;
this.bidPrice = 9.99;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
this.time = sdf.format(date);
}
public TickData(TickData oldTickData){
this.symbol=oldTickData.symbol;
Date date = new Date();
//年月日 时分秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
this.time = sdf.format(date);
long seed = System.currentTimeMillis();
Random rint = new Random(seed);
double delta = (rint.nextInt(3)-1.0)/100.0;
this.askPrice=oldTickData.askPrice+delta;
this.bidPrice=oldTickData.bidPrice+delta;
}
}
测试esper
package com.yss.fengxin;
import com.espertech.esper.common.client.EPCompiled;
import com.espertech.esper.common.client.configuration.Configuration;
import com.espertech.esper.compiler.client.CompilerArguments;
import com.espertech.esper.compiler.client.EPCompileException;
import com.espertech.esper.compiler.client.EPCompiler;
import com.espertech.esper.compiler.client.EPCompilerProvider;
import com.espertech.esper.runtime.client.EPDeployException;
import com.espertech.esper.runtime.client.EPDeployment;
import com.espertech.esper.runtime.client.EPEventService;
import com.espertech.esper.runtime.client.EPRuntime;
import com.espertech.esper.runtime.client.EPRuntimeProvider;
import com.espertech.esper.runtime.client.EPStatement;
public class TestEsper {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//初始化complier
EPCompiler compiler = EPCompilerProvider.getCompiler();
Configuration configuration = new Configuration();
configuration.getCommon().addEventType(TickData.class);
CompilerArguments complierArgs = new CompilerArguments(configuration);
//编译EPL
EPCompiled epCompiled;
try {
epCompiled = compiler.compile("@name('my-statement') select symbol, askPrice, bidPrice, time from TickData", complierArgs);
}
catch (EPCompileException ex) {
// handle exception here
throw new RuntimeException(ex);
}
//初始化运行时环境
EPRuntime epRuntime = EPRuntimeProvider.getDefaultRuntime(configuration);
EPDeployment deployment;
try {
deployment = epRuntime.getDeploymentService().deploy(epCompiled);
}
catch (EPDeployException ex) {
// handle exception here
throw new RuntimeException(ex);
}
EPStatement epStatement = epRuntime.getDeploymentService().getStatement(deployment.getDeploymentId(), "my-statement");
epStatement.addListener( (newData, oldData, statement, runtime) -> {
String symbol = (String) newData[0].get("symbol");
double askPrice = (double) newData[0].get("askPrice");
double bidPrice = (double) newData[0].get("bidPrice");
String time = (String) newData[0].get("time");
System.out.println(String.format("symbol: %s, askPrice: %f, bidPrice: %f, time: %s ", symbol, askPrice,bidPrice,time));
});
EPEventService epEventService = epRuntime.getEventService();
//定义初始事件对象 用于迭代构造新事件对象
TickData tickData0 = new TickData();
//循环模拟事件流
for (int i = 0; i < 10; i++) {
TickData dataTick = new TickData(tickData0);
epEventService.sendEventBean(dataTick,"TickData");
}
}
}
运行会有相应的结果显示。
参考资料:官方documentation esper_reference.pdf