aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-11-20 20:02:47 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2012-11-20 20:02:47 +0100
commitcac8228f38136cfc41673458c58c25f168b1e1ff (patch)
tree34f482cfd506a3b217f9f62fc00719b7f36e4a9e /src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java
parent8596b9b566745ca65b3a75fe8b6d4c091369fedc (diff)
downloadesper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.tar.gz
esper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.tar.bz2
esper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.tar.xz
esper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.zip
o Adding BoneCP init to Config.
o Starting on an actor-like structure for the running jobs. o Loading Nexus servers and group ids to look for from the database.
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java')
-rw-r--r--src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java b/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java
new file mode 100644
index 0000000..6b6fe75
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java
@@ -0,0 +1,69 @@
+package io.trygvis.esper.testing.object;
+
+import javax.sql.*;
+import java.io.*;
+import java.sql.*;
+
+public class ObjectUtil {
+
+ public static <A extends TransactionalActor> ActorRef<A> threadedActor(DataSource dataSource, long delay, A actor) {
+ return new ThreadedActor<>(dataSource, actor, delay);
+ }
+
+ static class ThreadedActor<A extends TransactionalActor> implements ActorRef<A>, Runnable, Closeable {
+
+ private final DataSource dataSource;
+ private final A actor;
+ private final long delay;
+ private final Thread thread;
+ private boolean shouldRun = true;
+
+ ThreadedActor(DataSource dataSource, A actor, long delay) {
+ this.dataSource = dataSource;
+ this.actor = actor;
+ this.delay = delay;
+ thread = new Thread(this);
+ thread.setDaemon(true);
+ thread.start();
+ }
+
+ public A underlying() {
+ return actor;
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ public void run() {
+ while (shouldRun) {
+ try {
+ try (Connection c = dataSource.getConnection()) {
+ try {
+ actor.act(c);
+ } finally {
+ c.rollback();
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace(System.out);
+ }
+
+ try {
+ Thread.sleep(delay);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }
+ }
+
+ public void close() throws IOException {
+ shouldRun = false;
+ thread.interrupt();
+ while (thread.isAlive()) {
+ try {
+ thread.join();
+ } catch (InterruptedException e) {
+ continue;
+ }
+ }
+ }
+ }
+}