esper安裝與示例

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可以使用了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章