javacomplex-event-processingesper

How to set StatementId/Statement Name in Esper 8.5 while deploying the statement


In Esper version 5, we used to register the EPL statements and add listener using below code -

final EPStatement statement = admin.createEPL(epl, subsc.getId().toString(), subsc);
statement.addListener(createListenerAdapter(listenerCallback));

As per Esper documentation in version 8(http://www.espertech.com/category/esper-8/) we can write a utility method compileDeploy() to register the epl statements as below --

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

How do we pass the Statement Id/Statement Name here along with Deployment Id?

Later while fetching the Statement from EPDeploymentService, it seems we need to pass deployment id as well as Statement Name as below -

final EPStatement statement = epDeploymentService.getStatement(deploymentId, subsc.getId());

Obviously we are getting statement as Null here. Actually we need this to remove the statement from Esper runtime.

Can someone please guide me how to pass Statement Id/Statement Name in the compileDeploy() method? or anywhere else we need to pass the same?


Solution

  • There are at 3 ways.

    You can set the statement name in EPL:

    @name('mystatement') select * from ....
    

    You can set the statement name when compiling.

    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";
         }
    }
    

    You can set the statement name at deployment time.

    DeploymentOptions options = new DeploymentOptions();
    options.setStatementNameRuntime(new StatementNameRuntimeOption() {
        public String getStatementName(StatementNameRuntimeContext env) {
            return "mystatementname";
        }
    }));
    runtime.getDeploymentService().deploy(compiled, options);