java学习日志5——esper30分钟入门example

18 篇文章 0 订阅

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

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值