esper安装与示例

来源:互联网 发布:哑铃健身大全软件 编辑:程序博客网 时间:2024/06/17 23:07

转自http://blog.csdn.net/gjt19910817/article/details/8682728


1. esper的安装:

在 http://esper.codehaus.org/esper/download/download.html 这里下载esper压缩包。解压缩之后获得文件夹 esper-4.x.x,在该文件夹根目录有 esper-4.x.x.jar,这就是我们需要用到的esper库,将其加入到项目中。值得注意的是,使用esper不仅仅需要这个jar文件,还需要esper所依赖的其他的库。我们还需要将 esper-4.x.x/esper/lib 文件夹内的4个jar文件同样加入项目,这样才能正常使用 esper。


2. 详细安装方法:

已经安装好esper的同学可以直接跳至第三步。由于本人刚刚学习Java,对于Java的对于第三方库的使用非常不了解。所以费了老半天劲才将esper引入项目,其实很简单。(1) 将esper-4.x.x.jar 文件拖入eclipse的项目中,选择“Copy File”。(2) 右击项目名 => Properties => Libraries => Add JARs => 选择 esper-4.x.x.jar => OK ,这样,esper就安装好了。(3) 同样的办法引入 esper-4.x.x/esper/lib 文件夹内的另外4个jar文件。之后,esper就可以使用了。


3. esper简单实例

[java] view plaincopy
  1. import com.espertech.esper.client.*;  
  2. import java.util.Random;  
  3. import java.util.Date;  
  4.    
  5. public class exampleMain {  
  6.    
  7.     public static class Tick {  
  8.         String symbol;  
  9.         Double price;  
  10.         Date timeStamp;  
  11.    
  12.         public Tick(String s, double p, long t) {  
  13.             symbol = s;  
  14.             price = p;  
  15.             timeStamp = new Date(t);  
  16.         }  
  17.         public double getPrice() {return price;}  
  18.         public String getSymbol() {return symbol;}  
  19.         public Date getTimeStamp() {return timeStamp;}  
  20.    
  21.         @Override  
  22.         public String toString() {  
  23.             return "Price: " + price.toString() + " time: " + timeStamp.toString();  
  24.         }  
  25.     }  
  26.    
  27.     private static Random generator = new Random();  
  28.    
  29.     public static void GenerateRandomTick(EPRuntime cepRT) {  
  30.    
  31.         double price = (double) generator.nextInt(10);  
  32.         long timeStamp = System.currentTimeMillis();  
  33.         String symbol = "AAPL";  
  34.         Tick tick = new Tick(symbol, price, timeStamp);  
  35.         System.out.println("Sending tick:" + tick);  
  36.         cepRT.sendEvent(tick);  
  37.    
  38.     }  
  39.    
  40.     public static class CEPListener implements UpdateListener {  
  41.    
  42.         public void update(EventBean[] newData, EventBean[] oldData) {  
  43.             System.out.println("Event received: " + newData[0].getUnderlying());  
  44.         }  
  45.     }  
  46.    
  47.     public static void main(String[] args) {  
  48.    
  49. //The Configuration is meant only as an initialization-time object.  
  50.         Configuration cepConfig = new Configuration();  
  51.         cepConfig.addEventType("StockTick", Tick.class.getName());  
  52.         EPServiceProvider cep = EPServiceProviderManager.getProvider("myCEPEngine", cepConfig);  
  53.         EPRuntime cepRT = cep.getEPRuntime();  
  54.    
  55.         EPAdministrator cepAdm = cep.getEPAdministrator();  
  56.         EPStatement cepStatement = cepAdm.createEPL("select * from " +  
  57.                 "StockTick(symbol='AAPL').win:length(2) " +  
  58.                 "having avg(price) > 6.0");  
  59.    
  60.         cepStatement.addListener(new CEPListener());  
  61.    
  62.        // We generate a few ticks...  
  63.         for (int i = 0; i < 5; i++) {  
  64.             GenerateRandomTick(cepRT);  
  65.         }  
  66.     }  
  67. }  

输出:

[java] view plaincopy
  1. log4j:WARN No appenders could be found for logger (com.espertech.esper.epl.metric.MetricReportingPath).  
  2. log4j:WARN Please initialize the log4j system properly.  
  3. Sending tick:Price: 6.0 time: Tue Jul 21 01:11:15 CEST 2009  
  4. Sending tick:Price: 0.0 time: Tue Jul 21 01:11:15 CEST 2009  
  5. Sending tick:Price: 7.0 time: Tue Jul 21 01:11:15 CEST 2009  
  6. Sending tick:Price: 4.0 time: Tue Jul 21 01:11:15 CEST 2009  
  7. Sending tick:Price: 9.0 time: Tue Jul 21 01:11:15 CEST 2009  
  8. Event received: Price: 9.0 time: Tue Jul 21 01:11:15 CEST 2009  

好了。esper可以使用了。
0 0