I am new to Esper and trying to get old and new EventBean while updating data. However, I am getting old EventBean is always null. Is there any configuration missing due to the old EventBean being null?
public class TestApp1 {
public static void main(String[] arg) throws EPCompileException, EPDeployException {
EPCompiler compiler = EPCompilerProvider.getCompiler();
Configuration configuration = new Configuration();
configuration.getCommon().addEventType(PersonEvent.class);
CompilerArguments args = new CompilerArguments(configuration);
EPCompiled epCompiled = compiler.compile("@name('my-statement') select * from PersonEvent", args);
EPRuntime runtime = EPRuntimeProvider.getDefaultRuntime(configuration);
EPDeploymentService deploymentService = runtime.getDeploymentService();
EPDeployment deployment = deploymentService.deploy(epCompiled);
EPStatement statement = deploymentService.getStatement(deployment.getDeploymentId(), "my-statement");
statement.addListener((newData, oldData, stmt, rt) -> {
extracted(newData, "New");
extracted(oldData, "Old");
sleep(0);
});
publish(runtime);
}
private static void extracted(EventBean[] datas, String type) {
if (datas == null || datas.length == 0) {
System.out.println(type + " : " + datas);
return;
}
for(EventBean data : datas) {
String name = (String) data.get("name");
int age = (int) data.get("age");
System.out.printf(LocalTime.now() + "::" + type + " - Got event ---- Name: %s, Age: %d%n", name, age);
}
}
private static void sleep(int i) {
try {
TimeUnit.MILLISECONDS.sleep(i * 500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private static void publish(EPRuntime runtime) {
EPEventService eventService = runtime.getEventService();
eventService.sendEventBean(new PersonEvent("Peter", 10), "PersonEvent");
sleep(1);
eventService.sendEventBean(new PersonEvent("Peter", 11), "PersonEvent");
sleep(1);
eventService.sendEventBean(new PersonEvent("Peter", 12), "PersonEvent");
sleep(1);
eventService.sendEventBean(new PersonEvent("Hello", 6), "PersonEvent");
sleep(1);
eventService.sendEventBean(new PersonEvent("Peter", 13), "PersonEvent");
}
}
class PersonEvent {
private String name;
private int age;
public PersonEvent(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
and output of the above program is -
13:07:47.355::New - Got event ---- Name: Peter, Age: 10 Old : null
13:07:47.863::New - Got event ---- Name: Peter, Age: 11 Old : null
13:07:48.363::New - Got event ---- Name: Peter, Age: 12 Old : null
13:07:48.877::New - Got event ---- Name: Hello, Age: 6 Old : null
13:07:49.389::New - Got event ---- Name: Peter, Age: 13 Old : null
As per the documentation of UpdateListener -
I did not find a way to receive both values i.e. oldEvent and newEvent at the same time however we can get data from both events in newEvent. The following query is giving me the expected data -