1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package com.zx; import com.espertech.esper.client.*; import java.util.Random; import java.util.Date; public class test1 {
public static class Tick { String symbol; Double price; Date timeStamp; public Tick(String s, double p, long t) { symbol = s; price = p; timeStamp = new Date(t); }
public String getSymbol() { return symbol; }
public Double getPrice() { return price; }
public Date getTimeStamp() { return timeStamp; }
@Override public String toString() { return "价格:" + price .toString() + " 时间:" + timeStamp.toString(); } } private static Random generator = new Random(); public static void GenerateRandomTick(EPRuntime cepRT) { double price = ( double) generator.nextInt(10); long timeStamp = System. currentTimeMillis(); String symbol = "AAPL"; Tick tick = new Tick(symbol, price, timeStamp); System. out.println( "发送tick:" + tick); cepRT.sendEvent(tick); } public static class CEPListener implements UpdateListener { public void update(EventBean[] newData, EventBean[] oldData) { System. out.println( "[触发事件: " + newData[0].getUnderlying() + "]"); } } public static void main(String[] args) { Configuration cepConfig = new Configuration(); cepConfig.addEventType( "StockTick", Tick. class.getName()); EPServiceProvider cep = EPServiceProviderManager.getProvider( "myCEPEngine", cepConfig); EPRuntime cepRT = cep.getEPRuntime();
EPAdministrator cepAdm = cep.getEPAdministrator();
EPStatement cepStatement = cepAdm.createEPL( "select * from " + "StockTick(symbol='AAPL').win:length(2) " + "having avg(price) > 6.0" ); cepStatement.addListener( new CEPListener()); for (int i = 0; i < 20; i++) { GenerateRandomTick(cepRT); } } }
|