aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/trygvis/esper/testing/esper/GenericListener.java24
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/esper/Test1.java (renamed from src/main/java/io/trygvis/esper/testing/esper/Main.java)66
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/esper/Test2.java (renamed from src/main/java/io/trygvis/esper/testing/esper/Main2.java)19
3 files changed, 64 insertions, 45 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/esper/GenericListener.java b/src/main/java/io/trygvis/esper/testing/esper/GenericListener.java
new file mode 100644
index 0000000..c7922e0
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/esper/GenericListener.java
@@ -0,0 +1,24 @@
+package io.trygvis.esper.testing.esper;
+
+import com.espertech.esper.client.*;
+import org.slf4j.*;
+
+public class GenericListener implements UpdateListener {
+
+ private final Logger logger;
+
+ public GenericListener(Logger logger) {
+ this.logger = logger;
+ }
+
+ public void update(EventBean[] newEvents, EventBean[] oldEvents) {
+ for (EventBean event : newEvents) {
+ logger.info("new event");
+ EventType eventType = event.getEventType();
+
+ for (String name : eventType.getPropertyNames()) {
+ System.out.println(name + " = " + event.get(name));
+ }
+ }
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/esper/Main.java b/src/main/java/io/trygvis/esper/testing/esper/Test1.java
index 66b88df..842dcb3 100755
--- a/src/main/java/io/trygvis/esper/testing/esper/Main.java
+++ b/src/main/java/io/trygvis/esper/testing/esper/Test1.java
@@ -8,7 +8,7 @@ import java.util.*;
import static java.lang.Thread.sleep;
-public class Main {
+public class Test1 {
private static final String JDBC_URL = "jdbc:h2:mem:esper;DB_CLOSE_DELAY=-1";
// private static final String JDBC_URL = "jdbc:h2:tcp://127.0.0.1/esper;DB_CLOSE_DELAY=-1";
@@ -17,8 +17,7 @@ public class Main {
public static void main(String[] args) throws Exception {
Config.loadFromDisk();
- Main main = new Main();
- main.work();
+ new Test1().work();
}
private void work() throws Exception {
@@ -38,16 +37,25 @@ public class Main {
// String expression = "select avg(price) from OrderEvent.win:time(30 sec)";
// String expression = "select * from pattern [every b=NewBuildEvent() -> (timer:interval(3 seconds) and not NewBuildEvent(uuid = b.uuid))]";
+// String expression = "insert into BuildLastHour " +
+// "select job, " +
+// "sum(case result when 'SUCCESS' then 1 else 0 end) as successes, " +
+// "sum(case result when 'FAILURE' then 1 else 0 end) as failures " +
+// "from NewBuildEvent.win:time_batch(3 sec) group by job";
+
String expression = "insert into BuildLastHour " +
- "select uuid, " +
+ "select job, " +
"sum(case result when 'SUCCESS' then 1 else 0 end) as successes, " +
"sum(case result when 'FAILURE' then 1 else 0 end) as failures " +
- "from NewBuildEvent.win:time_batch(3 sec) group by uuid";
+ "from NewBuildEvent.win:length(10) " +
+ "group by job " +
+ "having " +
+ "sum(case result when 'SUCCESS' then 1 else 0 end) > 1 AND " +
+ "count(result) > 3";
EPStatement statement = administrator.createEPL(expression);
- MyListener listener = new MyListener();
- statement.addListener(listener);
+ statement.addListener(new GenericListener(logger));
UUID job1 = UUID.fromString("11111111-1111-1111-1111-111111111111");
UUID job2 = UUID.fromString("22222222-2222-2222-2222-222222222222");
@@ -56,6 +64,7 @@ public class Main {
send(job2, "SUCCESS"); sleep(100);
send(job2, "FAILURE"); sleep(100);
send(job2, "SUCCESS"); sleep(100);
+ send(job2, "SUCCESS"); sleep(100);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@@ -72,37 +81,30 @@ public class Main {
public void send(UUID job, String result) {
logger.info("Inserting " + result);
- runtime.sendEvent(new NewBuildEvent(job, result));
+ runtime.sendEvent(new NewBuildEvent(job, UUID.randomUUID(), result));
}
- class MyListener implements UpdateListener {
- public void update(EventBean[] newEvents, EventBean[] oldEvents) {
- for (EventBean event : newEvents) {
- logger.info("new event");
- EventType eventType = event.getEventType();
+ class NewBuildEvent {
+ private UUID job;
+ private UUID uuid;
+ private String result;
- for (String name : eventType.getPropertyNames()) {
- System.out.println(name + " = " + event.get(name));
- }
- }
+ NewBuildEvent(UUID job, UUID uuid, String result) {
+ this.job = job;
+ this.uuid = uuid;
+ this.result = result;
}
- }
-}
-
-class NewBuildEvent {
- private UUID uuid;
- private String result;
- NewBuildEvent(UUID uuid, String result) {
- this.uuid = uuid;
- this.result = result;
- }
+ public UUID getJob() {
+ return job;
+ }
- public UUID getUuid() {
- return uuid;
- }
+ public UUID getUuid() {
+ return uuid;
+ }
- public String getResult() {
- return result;
+ public String getResult() {
+ return result;
+ }
}
}
diff --git a/src/main/java/io/trygvis/esper/testing/esper/Main2.java b/src/main/java/io/trygvis/esper/testing/esper/Test2.java
index 9c53a8f..ecd276a 100755
--- a/src/main/java/io/trygvis/esper/testing/esper/Main2.java
+++ b/src/main/java/io/trygvis/esper/testing/esper/Test2.java
@@ -2,12 +2,13 @@ package io.trygvis.esper.testing.esper;
import com.espertech.esper.client.*;
import io.trygvis.esper.testing.*;
+import org.slf4j.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
-public class Main2 {
+public class Test2 {
private static Config config;
@SuppressWarnings("UnusedDeclaration")
@@ -21,7 +22,7 @@ public class Main2 {
Configuration c = new Configuration();
ConfigurationDBRef configurationDBRef = new ConfigurationDBRef();
- configurationDBRef.setDataSourceFactory(new Properties(), Main2.class.getName());
+ configurationDBRef.setDataSourceFactory(new Properties(), Test2.class.getName());
configurationDBRef.setConnectionAutoCommit(true);
c.addDatabaseReference("db1", configurationDBRef);
@@ -39,8 +40,9 @@ public class Main2 {
"select uuid, job, result from pattern [every timer:interval(1 sec)], " +
"sql:db1 ['SELECT uuid, job, result FROM jenkins_build WHERE extract(epoch from created_date) > ${VarLastTimestamp}']");
- JenkinsBuildListener listener = new JenkinsBuildListener();
- statement.addListener(listener);
+ Logger logger = LoggerFactory.getLogger("app");
+
+ statement.addListener(new GenericListener(logger));
administrator.createEPL("on JenkinsBuild set VarLastTimestamp = current_timestamp()");
@@ -52,14 +54,6 @@ public class Main2 {
}
}
-class JenkinsBuildListener implements UpdateListener {
- public void update(EventBean[] newEvents, EventBean[] oldEvents) {
- for (EventBean event : newEvents) {
- System.out.println("uuid=" + event.get("uuid"));
- }
- }
-}
-
/*
every build=JenkinsBuild(result='FAILURE') ->
(
@@ -73,5 +67,4 @@ every build=JenkinsBuild(result='FAILURE') ->
)
)
where timer:within(90 seconds))
-
*/