在Esper中替代setSubscriber

时间:2017-12-26 08:31:08

标签: java annotations esper subscriber epl

我正在尝试将订阅者附加到Esper中的事件,但我想使用.epl文件。我一直在浏览存储库,我已经看到了使用注释接口执行此操作的示例。我试图像他们在CoinTrader中那样做,但我似乎无法让它发挥作用。但是,如果我将订户设置为Java,它就可以工作。

This is my project structure for reference

这是我的.epl文件:

module queries;

import events.*;
import configDemo.*;
import annotations.*;

create schema MyTickEvent as TickEvent;

@Name('allEvents')
@Description('test')
@Subscriber(className='configDemo.TickSubscriber')
select * from TickEvent; 


@Name('tickEvent')
@Description('Get a tick event every 3 seconds')
select currentPrice from TickEvent;

这是我的配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<esper-configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.espertech.com/schema/esper"
xsi:noNamespaceSchemaLocation="esper-configuration-6-0.xsd">


<event-type-auto-name package-name="events"/>

<auto-import import-name="annotations.*"/>
<auto-import import-name="events.*"/>
<auto-import import-name="configDemo.*"/>

这是我的订阅者界面:

package annotations;

public @interface Subscriber {

String className();
}

这是我的活动类:

package configDemo;

import events.TickEvent;

public class TickSubscriber {
public void update(TickEvent tick) {
    System.out.println("Event registered by subscriber  - Tick is: " + 
tick.getCurrentPrice());
}
}

我的主要档案是:

 package configDemo;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.deploy.DeploymentException;
import com.espertech.esper.client.deploy.DeploymentOptions;
import com.espertech.esper.client.deploy.Module;
import com.espertech.esper.client.deploy.ParseException;

public class Main {

    public static EngineHelper engineHelper; 
    public static Thread engineThread;
    public static boolean continuousSimulation = true;

    public static void main(String[] args) throws DeploymentException, InterruptedException, IOException, ParseException {

        engineHelper = new EngineHelper();
        DeploymentOptions options = new DeploymentOptions();
        options.setIsolatedServiceProvider("validation"); // we isolate any statements
        options.setValidateOnly(true); // validate leaving no started statements
        options.setFailFast(false); // do not fail on first error

        Module queries = engineHelper.getDeployAdmin().read("queries.epl");
        engineHelper.getDeployAdmin().deploy(queries, null);

        CountDownLatch latch = new CountDownLatch(1);

        EPStatement epl = engineHelper.getAdmin().getStatement("allEvents");
        //epl.setSubscriber(new TickSubscriber());
        engineThread = new Thread(new EngineThread(latch, continuousSimulation, engineHelper.getRuntime()));
        engineThread.start();


    }



}

正如您所看到的,setSubscriber行被注释掉了。当我按原样运行时,我预计订户将被识别并注册,但事实并非如此。我只得到控制台中的滴答事件。如果我退出该行并运行它,我会在订阅者收到该事件的每个滴答之后收到通知,并且一切正常。

我做错了什么?如何在.epl文件中设置订户?

1 个答案:

答案 0 :(得分:0)

分配订阅者是由应用程序完成的,而不是引擎为您执行的操作。应用程序代码需要循环通过语句,获取注释“stmt.getAnnotations”并检查这些并分配订阅者。