From cac8228f38136cfc41673458c58c25f168b1e1ff Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 20 Nov 2012 20:02:47 +0100 Subject: 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. --- .../trygvis/esper/testing/object/ObjectUtil.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java (limited to 'src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java') 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 ActorRef threadedActor(DataSource dataSource, long delay, A actor) { + return new ThreadedActor<>(dataSource, actor, delay); + } + + static class ThreadedActor implements ActorRef, 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; + } + } + } + } +} -- cgit v1.2.3