Created
April 20, 2013 00:42
-
-
Save chrisfesler/5424196 to your computer and use it in GitHub Desktop.
strange behavior from esper when invoking methods on an Object variable while using a context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.apexclearing.esperdemo; | |
import com.espertech.esper.client.*; | |
import java.util.*; | |
public class Demo { | |
private final static String[] demoStatements = new String[]{ | |
" CREATE CONTEXT partcontext PARTITION BY part FROM msg" | |
, | |
" CREATE VARIABLE com.apexclearing.esperdemo.Demo demo = com.apexclearing.esperdemo.Demo.make()" | |
, | |
" CONTEXT partcontext\n" + | |
" SELECT *\n" + | |
" FROM PATTERN [\n" + | |
" EVERY (\n" + | |
" firstMsg=msg(\n" + | |
" demo.log(' init', part, filt, msgId)\n" + | |
" )\n" + | |
" -> (\n" + | |
" [1:] rest=msg(\n" + | |
" filt = firstMsg.filt\n" + | |
" AND demo.log(' rest', part, filt, msgId)\n" + | |
" )\n" + | |
" until (\n" + | |
" msg(\n" + | |
" filt != firstMsg.filt\n" + | |
" AND demo.log('until', part, filt, msgId)\n" + | |
" )\n" + | |
" )\n" + | |
" )\n" + | |
" )\n" + | |
" ]" | |
}; | |
/** | |
* Factory method so we can assign our variable in EPL | |
*/ | |
public static Demo make() { | |
return new Demo(); | |
} | |
/** | |
* Demonstrates weird behavior when invoked from EPL | |
*/ | |
public boolean log(String from, String id, String filt, int msgId) { | |
System.out.println("{ from: " + from + ", id: " + id + ", filt: " + filt + ", msgId: " + msgId + " }"); | |
return true; | |
} | |
public static void main(String[] args) throws Exception { | |
Map<String, Object> schema = new HashMap<String, Object>(); | |
schema.put("part", String.class); | |
schema.put("filt", String.class); | |
schema.put("msgId", int.class); | |
List<Map<String, Object>> events = new ArrayList<>(); | |
// part, filt, msgId | |
events.add(makeEvent("foo", "y", 101)); | |
events.add(makeEvent("bar", "n", 102)); | |
events.add(makeEvent("bar", "y", 103)); | |
events.add(makeEvent("bar", "y", 104)); | |
events.add(makeEvent("bar", "n", 105)); | |
new DemoRunner(schema, demoStatements, events).run(); | |
} | |
private static Map<String, Object> makeEvent(String part, String filt, int msgId) { | |
Map<String, Object> event = new HashMap<>(); | |
event.put("part", part); | |
event.put("filt", filt); | |
event.put("msgId", msgId); | |
return event; | |
} | |
private static class DemoRunner { | |
private final EPServiceProvider provider; | |
private final String[] statements; | |
private final List<Map<String, Object>> events; | |
public DemoRunner(Map<String, Object> schema, String[] statements, List<Map<String, Object>> events) { | |
this.statements = statements; | |
this.events = events; | |
Configuration esperConfig = new Configuration(); | |
provider = EPServiceProviderManager.getProvider(UUID.randomUUID().toString(), esperConfig); | |
ConfigurationOperations providerOptions = provider.getEPAdministrator().getConfiguration(); | |
providerOptions.addEventType("msg", schema); | |
} | |
private void run() throws Exception { | |
EPRuntime epRuntime = provider.getEPRuntime(); | |
EPAdministrator epAdministrator = provider.getEPAdministrator(); | |
for (String stmt : statements) { | |
epAdministrator.createEPL(stmt); | |
} | |
for (Map<String, Object> event : events) { | |
epRuntime.sendEvent(event, "msg"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# we see a bunch of things that don't correspond to actual | |
# messages, and we keep seeing id: "foo" up until the end, | |
# despite the fact that he have matched only the firstMsg | |
# guy, in the "foo" context, and should never have appeared | |
# in the "bar" context. | |
{ from: init, id: foo, filt: y, msgId: 101 } | |
{ from: until, id: foo, filt: n, msgId: 102 } | |
{ from: until, id: foo, filt: n, msgId: 102 } | |
{ from: init, id: bar, filt: n, msgId: 102 } | |
{ from: until, id: bar, filt: y, msgId: 103 } | |
{ from: rest, id: foo, filt: y, msgId: 103 } | |
{ from: rest, id: foo, filt: y, msgId: 104 } | |
{ from: init, id: bar, filt: y, msgId: 104 } | |
{ from: until, id: foo, filt: n, msgId: 105 } | |
{ from: until, id: bar, filt: n, msgId: 105 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment