Skip to content

Instantly share code, notes, and snippets.

@alejolp
Created June 20, 2014 09:40
Show Gist options
  • Save alejolp/4f1cac757f8ab0e6354c to your computer and use it in GitHub Desktop.
Save alejolp/4f1cac757f8ab0e6354c to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<esper-configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.espertech.com/schema/esper"
xsi:schemaLocation=
"http://www.espertech.com/schema/esper/esper-configuration-2.0.xsd">
<engine-settings>
<defaults>
<metrics-reporting enabled="true" jmx-engine-metrics="true" engine-interval="1000" statement-interval="1000" threading="false"/>
</defaults>
</engine-settings>
</esper-configuration>
import java.util.Date;
import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPAdministrator;
import com.espertech.esper.client.EPRuntime;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;
import com.espertech.esper.client.time.CurrentTimeEvent;
import com.espertech.esper.client.time.CurrentTimeSpanEvent;
import com.espertech.esper.client.time.TimerControlEvent;
public class Main implements UpdateListener {
public static class TestEvent {
public TestEvent(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private EPServiceProvider esper;
private EPAdministrator epAdministrator;
private EPRuntime epRuntime;
private boolean externalTiming = true;
private long currentTimeExternal;
public void update(EventBean[] arg0, EventBean[] arg1) {
if (arg0 != null && arg0.length > 0)
System.out.println(new Date().toString() + " arg0: " + arg0[0]);
}
public void run() {
Configuration esperConfig = new Configuration();
esperConfig.configure(new java.io.File("esper.xml"));
esper = EPServiceProviderManager.getProvider("AsantosEsperTest",
esperConfig);
epAdministrator = esper.getEPAdministrator();
epRuntime = esper.getEPRuntime();
// This breaks metrics. For the metrics to work, the initial timing
// event on external timing should be system time at engine
// initialization.
currentTimeExternal = 0;
initializeExternalTiming();
initializeMetrics();
initializeEventType();
initializeStatements();
while (true) {
epRuntime.sendEvent(new TestEvent("QQQ"));
if (externalTiming) {
epRuntime.sendEvent(new CurrentTimeSpanEvent(
currentTimeExternal));
currentTimeExternal += 150 * 2;
}
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void initializeStatements() {
epAdministrator.createEPL("select * from TestEvent").addListener(this);
}
private void initializeMetrics() {
epAdministrator.createEPL(
"select * from com.espertech.esper.client.metric.EngineMetric")
.addListener(this);
epAdministrator
.createEPL(
"select * from com.espertech.esper.client.metric.StatementMetric")
.addListener(this);
}
private void initializeExternalTiming() {
if (externalTiming) {
epRuntime.sendEvent(new TimerControlEvent(
TimerControlEvent.ClockType.CLOCK_EXTERNAL));
epRuntime.sendEvent(new CurrentTimeEvent(currentTimeExternal));
}
}
private void initializeEventType() {
epAdministrator.getConfiguration().addEventType(TestEvent.class);
}
public static void main(String[] args) {
Main m = new Main();
m.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment