Search code examples
complex-event-processingesperepl

Esper: EPL "select" failed du to a validation error


I'm trying to run a simple Test with Esper (Chapter 14, http://dl.e-book-free.com/2013/07/activiti_in_action.pdf). The code is pretty simple:

public class EventLengthWindowTest {
  public class LoanRequestEvent {
    public int amount =2;

    public LoanRequestEvent(int a){
        amount += a;
    }
}

private int sumAmount = 0;

@Test
public void testEventLengthWindow() {
  Configuration configuration = new Configuration();
  configuration.addEventType(LoanRequestEvent.class); 

  EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(configuration);
  EPAdministrator admin = epService.getEPAdministrator();
  EPStatement epStatement = admin.createEPL("select sum(amount) as sumAmount from LoanRequestEvent.win:length(2)");

  ...
}

I get an error message regarding the EPL part:

"select sum(amount) as sumAmount from LoanRequestEvent.win:length(2)"

It says:

com.espertech.esper.client.EPStatementException: Error starting statement: Failed to validate select-clause expression 'sum(amount)': Property named 'amount' is not valid in any stream [select sum(amount) as sumAmount from LoanRequestEvent.win:length(2)]

Any ideas why this happens?


Solution

  • You need to provide JavaBean getters and setters for the event properties in your event classes if you want Esper to read and/or write to them. For your example to work, you need to add a getter like this:

    public class LoanRequestEvent {
        public int amount =2;
    
        public LoanRequestEvent(int a){
            amount += a;
        }
    
        public int getAmount() {
            return amount;
        }
    }