java - 部署语句时如何在 Esper 8.5 中设置 StatementId/Statement Name
问题描述
在 Esper 版本 5 中,我们使用以下代码注册 EPL 语句并添加侦听器 -
final EPStatement statement = admin.createEPL(epl, subsc.getId().toString(), subsc);
statement.addListener(createListenerAdapter(listenerCallback));
根据版本 8 ( http://www.espertech.com/category/esper-8/ ) 中的 Esper 文档,我们可以编写一个实用方法 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 Id/Statement Name 以及 Deployment Id?
稍后在从 EPDeploymentService 获取语句时,似乎我们需要传递部署 ID 以及语句名称,如下所示 -
final EPStatement statement = epDeploymentService.getStatement(deploymentId, subsc.getId());
显然,我们在这里得到了 Null 的声明。实际上,我们需要这个来从 Esper 运行时中删除该语句。
有人可以指导我如何在 compileDeploy() 方法中传递 Statement Id/Statement 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);
推荐阅读
- excel - 如何抑制来自 Excel 的 HTML 'HEAD' 请求
- iframe - 如何最大化嵌入式 Google 表格或文档的大小、宽度和高度?
- typescript - 将遗留 JS 包含到 Typescript 模块中
- python - 在没有 matplotlib 的 tkinter 中创建缩放时间线
- python - 返回与指针分配之间的 c_char_p 行为不一致
- html - 超链接图像
- python - 这个合并排序实现的时间复杂度是 O(nlogn) 吗?
- android - 如何更改弹出菜单中项目之间的边距
- android - Android - 通过edittext将内容添加到列表视图的简单应用程序
- python - 需要初始化的 tf.keras 自定义层无法使用 tf.contrib.saved_model.save_keras_model 保存