我正在尝试将订阅者附加到 Esper 中的事件,但我想为此使用 .epl 文件。我一直在浏览存储库,并且看到了使用注释接口的示例。我试图像他们在 CoinTrader 中那样做,但我似乎无法让它工作。然而,如果我在 Java 中设置订阅者,它就可以工作。
这是我的 .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 文件中设置订阅者?