From b65d39ab617d19ac48f44bc41f04a18803ca75e6 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 26 May 2013 10:42:01 +0200 Subject: o Adding a 'parent' reference so tasks can be put in a tree. --- src/main/java/io/trygvis/queue/AsyncService.java | 2 ++ .../java/io/trygvis/queue/JdbcAsyncService.java | 12 +++++++++-- src/main/java/io/trygvis/queue/Task.java | 10 +++++++--- src/main/java/io/trygvis/queue/TaskDao.java | 23 +++++++++++++--------- 4 files changed, 33 insertions(+), 14 deletions(-) (limited to 'src/main/java/io/trygvis') diff --git a/src/main/java/io/trygvis/queue/AsyncService.java b/src/main/java/io/trygvis/queue/AsyncService.java index 41bb75a..c9e5861 100755 --- a/src/main/java/io/trygvis/queue/AsyncService.java +++ b/src/main/java/io/trygvis/queue/AsyncService.java @@ -19,6 +19,8 @@ public interface AsyncService { Task schedule(Queue queue, String... args); + Task schedule(long parent, Queue queue, String... args); + /** * Polls for a new state of the execution. */ diff --git a/src/main/java/io/trygvis/queue/JdbcAsyncService.java b/src/main/java/io/trygvis/queue/JdbcAsyncService.java index 06e7eee..276541f 100644 --- a/src/main/java/io/trygvis/queue/JdbcAsyncService.java +++ b/src/main/java/io/trygvis/queue/JdbcAsyncService.java @@ -94,6 +94,14 @@ public class JdbcAsyncService implements AsyncService { @Transactional(propagation = REQUIRED) public Task schedule(final Queue queue, String... args) { + return scheduleInner(null, queue, args); + } + + public Task schedule(long parent, Queue queue, String... args) { + return scheduleInner(parent, queue, args); + } + + private Task scheduleInner(Long parent, final Queue queue, String... args) { Date scheduled = new Date(); StringBuilder arguments = new StringBuilder(); @@ -101,8 +109,8 @@ public class JdbcAsyncService implements AsyncService { arguments.append(arg).append(' '); } - long id = taskDao.insert(queue.name, scheduled, arguments.toString()); - Task task = new Task(id, queue.name, scheduled, null, 0, null, asList(args)); + long id = taskDao.insert(parent, queue.name, scheduled, arguments.toString()); + Task task = new Task(parent, id, queue.name, scheduled, null, 0, null, asList(args)); log.info("Created task = {}", task); registerSynchronization(new TransactionSynchronizationAdapter() { diff --git a/src/main/java/io/trygvis/queue/Task.java b/src/main/java/io/trygvis/queue/Task.java index 9a2e65b..09d5060 100755 --- a/src/main/java/io/trygvis/queue/Task.java +++ b/src/main/java/io/trygvis/queue/Task.java @@ -7,6 +7,8 @@ public class Task { public final long id; + public final Long parent; + public final String queue; public final Date scheduled; @@ -19,8 +21,9 @@ public class Task { public final List arguments; - Task(long id, String queue, Date scheduled, Date lastRun, int runCount, Date completed, List arguments) { + Task(long id, Long parent, String queue, Date scheduled, Date lastRun, int runCount, Date completed, List arguments) { this.id = id; + this.parent = parent; this.queue = queue; this.scheduled = scheduled; this.lastRun = lastRun; @@ -31,16 +34,17 @@ public class Task { } public Task registerRun() { - return new Task(id, queue, scheduled, new Date(), runCount + 1, completed, arguments); + return new Task(id, parent, queue, scheduled, new Date(), runCount + 1, completed, arguments); } public Task registerComplete(Date completed) { - return new Task(id, queue, scheduled, lastRun, runCount, completed, arguments); + return new Task(id, parent, queue, scheduled, lastRun, runCount, completed, arguments); } public String toString() { return "Task{" + "id=" + id + + ", parent=" + parent + ", queue=" + queue + ", scheduled=" + scheduled + ", lastRun=" + lastRun + diff --git a/src/main/java/io/trygvis/queue/TaskDao.java b/src/main/java/io/trygvis/queue/TaskDao.java index dac99c7..a59dcbb 100644 --- a/src/main/java/io/trygvis/queue/TaskDao.java +++ b/src/main/java/io/trygvis/queue/TaskDao.java @@ -21,10 +21,14 @@ public class TaskDao { @Autowired private JdbcTemplate jdbcTemplate; - @Transactional(propagation = MANDATORY) public long insert(String queue, Date scheduled, String arguments) { - jdbcTemplate.update("INSERT INTO task(id, run_count, queue, scheduled, arguments) " + - "VALUES(nextval('task_seq'), 0, ?, ?, ?)", queue, scheduled, arguments); + return this.insert(null, queue, scheduled, arguments); + } + + @Transactional(propagation = MANDATORY) + public long insert(Long parent, String queue, Date scheduled, String arguments) { + jdbcTemplate.update("INSERT INTO task(id, parent, run_count, queue, scheduled, arguments) " + + "VALUES(nextval('task_seq'), ?, 0, ?, ?, ?)", parent, queue, scheduled, arguments); return jdbcTemplate.queryForObject("SELECT currval('task_seq')", Long.class); } @@ -47,17 +51,18 @@ public class TaskDao { } private class TaskRowMapper implements RowMapper { - public static final String fields = "id, queue, scheduled, last_run, run_count, completed, arguments"; + public static final String fields = "id, parent, queue, scheduled, last_run, run_count, completed, arguments"; public Task mapRow(ResultSet rs, int rowNum) throws SQLException { - String arguments = rs.getString(7); + String arguments = rs.getString(8); return new Task( rs.getLong(1), - rs.getString(2), - rs.getTimestamp(3), + rs.getLong(2), + rs.getString(3), rs.getTimestamp(4), - rs.getInt(5), - rs.getTimestamp(6), + rs.getTimestamp(5), + rs.getInt(6), + rs.getTimestamp(7), arguments != null ? asList(arguments.split(" ")) : Collections.emptyList()); } } -- cgit v1.2.3