diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-06-02 12:32:29 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-06-02 12:32:29 +0200 |
commit | 52084f7b4e6f50c90b3255cdf2eb9deab560c970 (patch) | |
tree | eed9abd7fe9825aaacfd4fe24c8fd363cc41fed1 /src/main/java/io/trygvis/queue | |
parent | 7d704feb86c44fca57941d223e8605b55fcf68f0 (diff) | |
download | quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.tar.gz quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.tar.bz2 quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.tar.xz quartz-based-queue-52084f7b4e6f50c90b3255cdf2eb9deab560c970.zip |
o Making some test cases.
Diffstat (limited to 'src/main/java/io/trygvis/queue')
-rw-r--r-- | src/main/java/io/trygvis/queue/QueueDao.java | 47 | ||||
-rw-r--r-- | src/main/java/io/trygvis/queue/TaskDao.java | 119 |
2 files changed, 105 insertions, 61 deletions
diff --git a/src/main/java/io/trygvis/queue/QueueDao.java b/src/main/java/io/trygvis/queue/QueueDao.java index 63dde2a..2f69e11 100644 --- a/src/main/java/io/trygvis/queue/QueueDao.java +++ b/src/main/java/io/trygvis/queue/QueueDao.java @@ -1,36 +1,45 @@ package io.trygvis.queue; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.stereotype.Component; - +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import static org.springframework.dao.support.DataAccessUtils.singleResult; - -@Component public class QueueDao { - @Autowired - private JdbcTemplate jdbcTemplate; + private final Connection connection; - public Queue findByName(String name) { - return singleResult(jdbcTemplate.query("SELECT name, interval FROM queue WHERE name=?", new QueueRowMapper(), name)); + public QueueDao(Connection connection) { + this.connection = connection; } - public void insert(Queue q) { - jdbcTemplate.update("INSERT INTO queue(name, interval) VALUES(?, ?)", q.name, q.interval); + public Queue findByName(String name) throws SQLException { + try (PreparedStatement stmt = connection.prepareStatement("SELECT name, interval FROM queue WHERE name=?")) { + stmt.setString(1, name); + ResultSet rs = stmt.executeQuery(); + return rs.next() ? mapRow(rs) : null; + } } - public void update(Queue q) { - jdbcTemplate.update("UPDATE queue SET interval=? WHERE name=?", q.interval, q.name); + public void insert(Queue q) throws SQLException { + try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO queue(name, interval) VALUES(?, ?)")) { + int i = 1; + stmt.setString(i++, q.name); + stmt.setLong(i, q.interval); + stmt.executeUpdate(); + } } - private class QueueRowMapper implements RowMapper<Queue> { - public Queue mapRow(ResultSet rs, int rowNum) throws SQLException { - return new Queue(rs.getString(1), rs.getLong(2)); + public void update(Queue q) throws SQLException { + try (PreparedStatement stmt = connection.prepareStatement("UPDATE queue SET interval=? WHERE name=?")) { + int i = 1; + stmt.setLong(i++, q.interval); + stmt.setString(i, q.name); + stmt.executeUpdate(); } } + + public Queue mapRow(ResultSet rs) throws SQLException { + return new Queue(rs.getString(1), rs.getLong(2)); + } } diff --git a/src/main/java/io/trygvis/queue/TaskDao.java b/src/main/java/io/trygvis/queue/TaskDao.java index a59dcbb..5459933 100644 --- a/src/main/java/io/trygvis/queue/TaskDao.java +++ b/src/main/java/io/trygvis/queue/TaskDao.java @@ -1,69 +1,104 @@ package io.trygvis.queue; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import static java.util.Arrays.asList; -import static org.springframework.transaction.annotation.Propagation.MANDATORY; -@Component public class TaskDao { - @Autowired - private JdbcTemplate jdbcTemplate; + private final Connection connection; + + public static final String fields = "id, parent, queue, scheduled, last_run, run_count, completed, arguments"; - public long insert(String queue, Date scheduled, String arguments) { - return this.insert(null, queue, scheduled, arguments); + public TaskDao(Connection connection) { + this.connection = connection; } - @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); + public long insert(String queue, Date scheduled, String arguments) throws SQLException { + return insert(null, queue, scheduled, arguments); } - @Transactional(propagation = MANDATORY) - public Task findById(long id) { - return jdbcTemplate.queryForObject("SELECT " + TaskRowMapper.fields + " FROM task WHERE id=?", - new TaskRowMapper(), id); + public long insert(Long parent, String queue, Date scheduled, String arguments) throws SQLException { + String sql = "INSERT INTO task(id, parent, run_count, queue, scheduled, arguments) " + + "VALUES(nextval('task_seq'), ?, 0, ?, ?, ?)"; + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + int i = 1; + if (parent == null) { + stmt.setNull(i++, Types.BIGINT); + } else { + stmt.setLong(i++, parent); + } + stmt.setString(i++, queue); + stmt.setTimestamp(i++, new Timestamp(scheduled.getTime())); + stmt.setString(i, arguments); + stmt.executeUpdate(); + } + try (PreparedStatement stmt = connection.prepareStatement("SELECT currval('task_seq')")) { + ResultSet rs = stmt.executeQuery(); + rs.next(); + return rs.getLong(1); + } } - @Transactional(propagation = MANDATORY) - public List<Task> findByNameAndCompletedIsNull(String name) { - return jdbcTemplate.query("SELECT " + TaskRowMapper.fields + " FROM task WHERE queue=? AND completed IS NULL", - new TaskRowMapper(), name); + public Task findById(long id) throws SQLException { + try (PreparedStatement stmt = connection.prepareStatement("SELECT " + fields + " FROM task WHERE id=?")) { + ResultSet rs = stmt.executeQuery(); + return rs.next() ? mapRow(rs) : null; + } } - @Transactional(propagation = MANDATORY) - public void update(Task task) { - jdbcTemplate.update("UPDATE task SET scheduled=?, last_run=?, run_count=?, completed=? WHERE id=?", - task.scheduled, task.lastRun, task.runCount, task.completed, task.id); + public List<Task> findByNameAndCompletedIsNull(String name) throws SQLException { + try (PreparedStatement stmt = connection.prepareStatement("SELECT " + fields + " FROM task WHERE queue=? AND completed IS NULL")) { + int i = 1; + stmt.setString(i, name); + ResultSet rs = stmt.executeQuery(); + List<Task> list = new ArrayList<>(); + while (rs.next()) { + list.add(mapRow(rs)); + } + return list; + } } - private class TaskRowMapper implements RowMapper<Task> { - public static final String fields = "id, parent, queue, scheduled, last_run, run_count, completed, arguments"; + public void update(Task task) throws SQLException { + try (PreparedStatement stmt = connection.prepareStatement("UPDATE task SET scheduled=?, last_run=?, run_count=?, completed=? WHERE id=?")) { + int i = 1; + stmt.setTimestamp(i++, new Timestamp(task.scheduled.getTime())); + setTimestamp(stmt, i++, task.lastRun); + stmt.setInt(i++, task.runCount); + setTimestamp(stmt, i++, task.completed); + stmt.setLong(i, task.id); + stmt.executeUpdate(); + } + } - public Task mapRow(ResultSet rs, int rowNum) throws SQLException { - String arguments = rs.getString(8); - return new Task( - rs.getLong(1), - rs.getLong(2), - rs.getString(3), - rs.getTimestamp(4), - rs.getTimestamp(5), - rs.getInt(6), - rs.getTimestamp(7), - arguments != null ? asList(arguments.split(" ")) : Collections.<String>emptyList()); + private static void setTimestamp(PreparedStatement stmt, int parameterIndex, Date date) throws SQLException { + if (date == null) { + stmt.setNull(parameterIndex, Types.TIMESTAMP); + } else { + stmt.setTimestamp(parameterIndex, new Timestamp(date.getTime())); } } + + public Task mapRow(ResultSet rs) throws SQLException { + String arguments = rs.getString(8); + return new Task( + rs.getLong(1), + rs.getLong(2), + rs.getString(3), + rs.getTimestamp(4), + rs.getTimestamp(5), + rs.getInt(6), + rs.getTimestamp(7), + arguments != null ? asList(arguments.split(" ")) : Collections.<String>emptyList()); + } } |