在Esper版本5中,我们使用下面的代码来注册EPL语句并添加监听器------。
final EPStatement statement = admin.createEPL(epl, subsc.getId().toString(), subsc);
statement.addListener(createListenerAdapter(listenerCallback));
根据Esper在8版本的文档(http:/www.espertech.comcategoryesper-8)我们可以写一个实用方法compileDeploy()来注册epl语句,如下图所示----。
public EPDeployment compileDeploy(EPRuntime runtime, final String epl, final String deploymentId,final Subs subsc) {
try {
// Obtain a copy of the engine configuration
Configuration configuration = runtime.getConfigurationDeepCopy();
// Build compiler arguments
CompilerArguments args = new CompilerArguments(configuration);
// Make the existing EPL objects available to the compiler
args.getPath().add(runtime.getRuntimePath());
// Compile
EPCompiled compiled = EPCompilerProvider.getCompiler().compile(epl, args);
DeploymentOptions options = new DeploymentOptions();
options.setDeploymentId(deploymentId);
options.setStatementUserObjectRuntime(new StatementUserObjectRuntimeOption() {
public Object getUserObject(StatementUserObjectRuntimeContext env) {
return subsc;
}
});
// Return the deployment
return runtime.getDeploymentService().deploy(compiled, options);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
我们如何在这里将Statement IdStatement Name与Deployment Id一起传递?
后来,当我们从EPDeploymentService获取声明时,似乎我们需要传递部署ID以及声明名称,如下所示。
final EPStatement statement = epDeploymentService.getStatement(deploymentId, subsc.getId());
很明显,我们在这里得到的是Null语句。实际上,我们需要这样做来从Esper运行时删除该语句,谁能指导我如何在compileDeploy()方法中传递Statement IdStatement名称?
谁能指导我如何在compileDeploy()方法中传递Statement IdStatement Name? 或者我们需要在其他地方传递同样的内容?
有3种方法。
你可以在EPL中设置语句名。
@name('mystatement') select * from ....
你可以在编译时设置语句名。
CompilerArguments args = new CompilerArguments();
args.getOptions().setStatementName(new MyStatementNameResolver());
String epl = "select * from ....";
EPCompiled compiled = env.compile(epl, args);
private static class MyStatementNameResolver implements StatementNameOption {
public String getValue(StatementNameContext env) {
return "mystatement";
}
}
你可以在部署时设置语句名。
DeploymentOptions options = new DeploymentOptions();
options.setStatementNameRuntime(new StatementNameRuntimeOption() {
public String getStatementName(StatementNameRuntimeContext env) {
return "mystatementname";
}
}));
runtime.getDeploymentService().deploy(compiled, options);