1. 程式人生 > >esper安裝與示例

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簡單例項

import com.espertech.esper.client.*;
import java.util.Random;
import java.util.Date;
 
public class exampleMain {
 
    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 double getPrice() {return price;}
        public String getSymbol() {return symbol;}
        public Date getTimeStamp() {return timeStamp;}
 
        @Override
        public String toString() {
            return "Price: " + price.toString() + " time: " + 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("Sending tick:" + tick);
        cepRT.sendEvent(tick);
 
    }
 
    public static class CEPListener implements UpdateListener {
 
        public void update(EventBean[] newData, EventBean[] oldData) {
            System.out.println("Event received: " + newData[0].getUnderlying());
        }
    }
 
    public static void main(String[] args) {
 
//The Configuration is meant only as an initialization-time object.
        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());
 
       // We generate a few ticks...
        for (int i = 0; i < 5; i++) {
            GenerateRandomTick(cepRT);
        }
    }
}

輸出:
log4j:WARN No appenders could be found for logger (com.espertech.esper.epl.metric.MetricReportingPath).
log4j:WARN Please initialize the log4j system properly.
Sending tick:Price: 6.0 time: Tue Jul 21 01:11:15 CEST 2009
Sending tick:Price: 0.0 time: Tue Jul 21 01:11:15 CEST 2009
Sending tick:Price: 7.0 time: Tue Jul 21 01:11:15 CEST 2009
Sending tick:Price: 4.0 time: Tue Jul 21 01:11:15 CEST 2009
Sending tick:Price: 9.0 time: Tue Jul 21 01:11:15 CEST 2009
Event received: Price: 9.0 time: Tue Jul 21 01:11:15 CEST 2009

好了。esper可以使用了。