首页 > 解决方案 > 部署语句时如何在 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 吗?或者其他任何我们需要通过的地方?

标签: javacomplex-event-processingesper

解决方案


有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);

推荐阅读