From 54d7b2ce520e57cc0ffb9582546b80a32fa00682 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 12 Jun 2013 22:55:18 +0200 Subject: wip --- .../java/io/trygvis/test/PlainJavaExample.java | 126 --------------------- .../java/io/trygvis/test/PlainJavaExample2.java | 84 -------------- .../io/trygvis/test/jdbc/AsyncConsumerExample.java | 73 ++++++++++++ .../io/trygvis/test/jdbc/PlainJavaExample.java | 126 +++++++++++++++++++++ 4 files changed, 199 insertions(+), 210 deletions(-) delete mode 100644 src/test/java/io/trygvis/test/PlainJavaExample.java delete mode 100644 src/test/java/io/trygvis/test/PlainJavaExample2.java create mode 100644 src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java create mode 100644 src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java (limited to 'src/test') diff --git a/src/test/java/io/trygvis/test/PlainJavaExample.java b/src/test/java/io/trygvis/test/PlainJavaExample.java deleted file mode 100644 index cad8559..0000000 --- a/src/test/java/io/trygvis/test/PlainJavaExample.java +++ /dev/null @@ -1,126 +0,0 @@ -package io.trygvis.test; - -import io.trygvis.async.SqlEffect; -import io.trygvis.async.SqlEffectExecutor; -import io.trygvis.queue.JdbcQueueService; -import io.trygvis.queue.Queue; -import io.trygvis.queue.QueueService; -import io.trygvis.queue.QueueStats; -import io.trygvis.queue.QueueSystem; -import io.trygvis.queue.Task; -import io.trygvis.queue.TaskDao; -import io.trygvis.queue.TaskEffect; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Random; - -import static io.trygvis.queue.Task.TaskState; -import static io.trygvis.test.DbUtil.createDataSource; -import static java.lang.System.currentTimeMillis; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; - -public class PlainJavaExample { - private static final Random r = new Random(); - - private static String inputName = "my-input"; - private static String outputName = "my-output"; - - private static int interval = 10; - - public static class Consumer { - public static void main(String[] args) throws Exception { - System.out.println("Starting consumer"); - - DataSource ds = createDataSource(); - - SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); - - final QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); - final JdbcQueueService queueService = queueSystem.queueService; - - Queue[] queues = sqlEffectExecutor.transaction(new SqlEffect() { - @Override - public Queue[] doInConnection(Connection c) throws SQLException { - Queue[] queues = { - queueService.lookupQueue(c, inputName, interval, true), - queueService.lookupQueue(c, outputName, interval, true)}; - - TaskDao taskDao = queueSystem.createTaskDao(c); - - QueueStats stats = taskDao.findQueueStatsByName(inputName); - System.out.println("Queue stats for " + stats.name + ". Total number of tasks: " + stats.totalTaskCount); - for (Map.Entry entry : stats.states.entrySet()) { - System.out.println(entry.getKey() + " = " + entry.getValue()); - } - - return queues; - } - }); - - final Queue input = queues[0]; - final Queue output = queues[1]; - - QueueService.TaskExecutionRequest req = new QueueService.TaskExecutionRequest(1000, false); - - queueService.consumeAll(input, req, new TaskEffect() { - public List apply(Task task) throws Exception { - Long a = Long.valueOf(task.arguments.get(0)); - Long b = Long.valueOf(task.arguments.get(1)); - - System.out.println("a + b = " + a + " + " + b + " = " + (a + b)); - - if (r.nextInt(3000) > 0) { - return singletonList(task.childTask(output.name, new Date(), Long.toString(a + b))); - } - - throw new RuntimeException("Simulated exception while processing task."); - } - }); - System.out.println("Done"); - } - } - - public static class Producer { - public static void main(String[] args) throws Exception { - System.out.println("Starting producer"); - - int chunks = 100; - final int chunk = 10000; - - DataSource ds = createDataSource(); - - SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); - - QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); - final JdbcQueueService queueService = queueSystem.queueService; - - final Queue queue; - try (Connection c = ds.getConnection()) { - queue = queueService.lookupQueue(c, inputName, interval, true); - c.commit(); - } - - for (int i = 0; i < chunks; i++) { - long start = currentTimeMillis(); - sqlEffectExecutor.transaction(new SqlEffect.Void() { - @Override - public void doInConnection(Connection c) throws SQLException { - for (int j = 0; j < chunk; j++) { - queueService.schedule(c, queue, new Date(), asList("10", "20")); - } - } - }); - long end = currentTimeMillis(); - - long time = end - start; - System.out.println("Scheduled " + chunk + " tasks in " + time + "ms, " + (((double) chunk * 1000)) / ((double) time) + " chunks per second"); - } - } - } -} diff --git a/src/test/java/io/trygvis/test/PlainJavaExample2.java b/src/test/java/io/trygvis/test/PlainJavaExample2.java deleted file mode 100644 index faeebb2..0000000 --- a/src/test/java/io/trygvis/test/PlainJavaExample2.java +++ /dev/null @@ -1,84 +0,0 @@ -package io.trygvis.test; - -import io.trygvis.async.JdbcAsyncService; -import io.trygvis.async.SqlEffectExecutor; -import io.trygvis.queue.JdbcQueueService; -import io.trygvis.queue.Queue; -import io.trygvis.queue.QueueSystem; -import io.trygvis.queue.Task; -import io.trygvis.queue.TaskEffect; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.util.Date; -import java.util.List; - -import static io.trygvis.queue.Task.newTask; -import static io.trygvis.test.DbUtil.createDataSource; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; - -public class PlainJavaExample2 { - - private static String inputName = "my-input"; - private static String outputName = "my-output"; - - private static int interval = 10; - - private static final TaskEffect adder = new TaskEffect() { - public List apply(Task task) throws Exception { - System.out.println("PlainJavaExample$Consumer.consumeAll"); - Long a = Long.valueOf(task.arguments.get(0)); - Long b = Long.valueOf(task.arguments.get(1)); - - System.out.println("a + b = " + a + " + " + b + " = " + (a + b)); - - return singletonList(newTask(outputName, new Date(), Long.toString(a + b))); - } - }; - - public static class Consumer { - public static void main(String[] args) throws Exception { - System.out.println("Starting consumer"); - - DataSource ds = createDataSource(); - Connection c = ds.getConnection(); - - SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); - - QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); - JdbcQueueService queueService = queueSystem.queueService; - - final Queue input = queueService.lookupQueue(c, inputName, interval, true); - final Queue output = queueService.lookupQueue(c, outputName, interval, true); - - JdbcAsyncService asyncService = new JdbcAsyncService(queueSystem); - - asyncService.registerQueue(input, adder); - -// queueService.consumeAll(c, input, adder); - - c.commit(); - } - } - - public static class Producer { - public static void main(String[] args) throws Exception { - System.out.println("Starting producer"); - - DataSource ds = createDataSource(); - Connection c = ds.getConnection(); - - SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); - - QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); - JdbcQueueService queueService = queueSystem.queueService; - - Queue queue = queueService.lookupQueue(c, inputName, interval, true); - - queueService.schedule(c, queue, new Date(), asList("10", "20")); - - c.commit(); - } - } -} diff --git a/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java b/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java new file mode 100644 index 0000000..8d981f2 --- /dev/null +++ b/src/test/java/io/trygvis/test/jdbc/AsyncConsumerExample.java @@ -0,0 +1,73 @@ +package io.trygvis.test.jdbc; + +import io.trygvis.async.JdbcAsyncService; +import io.trygvis.async.SqlEffect; +import io.trygvis.async.SqlEffectExecutor; +import io.trygvis.queue.JdbcQueueService; +import io.trygvis.queue.Queue; +import io.trygvis.queue.QueueSystem; +import io.trygvis.queue.Task; +import io.trygvis.queue.TaskEffect; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; +import java.util.concurrent.ScheduledThreadPoolExecutor; + +import static io.trygvis.queue.Task.newTask; +import static io.trygvis.test.DbUtil.createDataSource; +import static java.util.Collections.singletonList; + +public class AsyncConsumerExample { + + private static String inputName = "my-input"; + private static String outputName = "my-output"; + + private static int interval = 10; + + private static final TaskEffect adder = new TaskEffect() { + public List apply(Task task) throws Exception { + Long a = Long.valueOf(task.arguments.get(0)); + Long b = Long.valueOf(task.arguments.get(1)); + + System.out.println("a + b = " + a + " + " + b + " = " + (a + b)); + + return singletonList(newTask(outputName, new Date(), Long.toString(a + b))); + } + }; + + public static class Consumer { + public static void main(String[] args) throws Exception { + System.out.println("Starting consumer"); + + DataSource ds = createDataSource(); + + SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); + + QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); + JdbcAsyncService asyncService = queueSystem.createAsyncService(); + final JdbcQueueService queueService = queueSystem.queueService; + + Queue[] queues = sqlEffectExecutor.transaction(new SqlEffect() { + @Override + public Queue[] doInConnection(Connection c) throws SQLException { + return new Queue[]{ + queueService.lookupQueue(c, inputName, interval, true), + queueService.lookupQueue(c, outputName, interval, true) + }; + } + }); + + final Queue input = queues[0]; + final Queue output = queues[1]; + + asyncService.registerQueue(input, adder); + + asyncService.startQueue(input, new ScheduledThreadPoolExecutor(2)); + Thread.sleep(5 * 1000); + asyncService.stopQueue(input); + } + } +} diff --git a/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java b/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java new file mode 100644 index 0000000..994c310 --- /dev/null +++ b/src/test/java/io/trygvis/test/jdbc/PlainJavaExample.java @@ -0,0 +1,126 @@ +package io.trygvis.test.jdbc; + +import io.trygvis.async.SqlEffect; +import io.trygvis.async.SqlEffectExecutor; +import io.trygvis.queue.JdbcQueueService; +import io.trygvis.queue.Queue; +import io.trygvis.queue.QueueService; +import io.trygvis.queue.QueueStats; +import io.trygvis.queue.QueueSystem; +import io.trygvis.queue.Task; +import io.trygvis.queue.TaskDao; +import io.trygvis.queue.TaskEffect; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Random; + +import static io.trygvis.queue.Task.TaskState; +import static io.trygvis.test.DbUtil.createDataSource; +import static java.lang.System.currentTimeMillis; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +public class PlainJavaExample { + private static final Random r = new Random(); + + private static String inputName = "my-input"; + private static String outputName = "my-output"; + + private static int interval = 10; + + public static class Consumer { + public static void main(String[] args) throws Exception { + System.out.println("Starting consumer"); + + DataSource ds = createDataSource(); + + SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); + + final QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); + final JdbcQueueService queueService = queueSystem.queueService; + + Queue[] queues = sqlEffectExecutor.transaction(new SqlEffect() { + @Override + public Queue[] doInConnection(Connection c) throws SQLException { + Queue[] queues = { + queueService.lookupQueue(c, inputName, interval, true), + queueService.lookupQueue(c, outputName, interval, true)}; + + TaskDao taskDao = queueSystem.createTaskDao(c); + + QueueStats stats = taskDao.findQueueStatsByName(inputName); + System.out.println("Queue stats for " + stats.name + ". Total number of tasks: " + stats.totalTaskCount); + for (Map.Entry entry : stats.states.entrySet()) { + System.out.println(entry.getKey() + " = " + entry.getValue()); + } + + return queues; + } + }); + + final Queue input = queues[0]; + final Queue output = queues[1]; + + QueueService.TaskExecutionRequest req = new QueueService.TaskExecutionRequest(1000, false); + + queueService.consumeAll(input, req, new TaskEffect() { + public List apply(Task task) throws Exception { + Long a = Long.valueOf(task.arguments.get(0)); + Long b = Long.valueOf(task.arguments.get(1)); + + System.out.println("a + b = " + a + " + " + b + " = " + (a + b)); + + if (r.nextInt(3000) > 0) { + return singletonList(task.childTask(output.name, new Date(), Long.toString(a + b))); + } + + throw new RuntimeException("Simulated exception while processing task."); + } + }); + System.out.println("Done"); + } + } + + public static class Producer { + public static void main(String[] args) throws Exception { + System.out.println("Starting producer"); + + int chunks = 100; + final int chunk = 10000; + + DataSource ds = createDataSource(); + + SqlEffectExecutor sqlEffectExecutor = new SqlEffectExecutor(ds); + + QueueSystem queueSystem = QueueSystem.initialize(sqlEffectExecutor); + final JdbcQueueService queueService = queueSystem.queueService; + + final Queue queue; + try (Connection c = ds.getConnection()) { + queue = queueService.lookupQueue(c, inputName, interval, true); + c.commit(); + } + + for (int i = 0; i < chunks; i++) { + long start = currentTimeMillis(); + sqlEffectExecutor.transaction(new SqlEffect.Void() { + @Override + public void doInConnection(Connection c) throws SQLException { + for (int j = 0; j < chunk; j++) { + queueService.schedule(c, queue, new Date(), asList("10", "20")); + } + } + }); + long end = currentTimeMillis(); + + long time = end - start; + System.out.println("Scheduled " + chunk + " tasks in " + time + "ms, " + (((double) chunk * 1000)) / ((double) time) + " chunks per second"); + } + } + } +} -- cgit v1.2.3