package io.trygvis.spring; import io.trygvis.queue.JdbcQueueService; import io.trygvis.queue.Queue; import io.trygvis.queue.QueueService; import io.trygvis.queue.QueueSystem; import io.trygvis.queue.TaskEffect; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.annotation.Transactional; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import java.util.List; public class SpringQueueService implements QueueService { public final JdbcTemplate jdbcTemplate; public JdbcQueueService queueService; public SpringQueueService(QueueSystem queueSystem, JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; this.queueService = queueSystem.queueService; } /** * @see JdbcQueueService#consumeAll(io.trygvis.queue.Queue, io.trygvis.queue.TaskEffect) */ public void consume(final Queue queue, final TaskEffect effect) throws SQLException { queueService.consumeAll(queue, effect); } @Transactional public Queue getQueue(final String name, final int interval, final boolean autoCreate) throws SQLException { return jdbcTemplate.execute(new ConnectionCallback() { @Override public Queue doInConnection(Connection c) throws SQLException, DataAccessException { return queueService.lookupQueue(c, name, interval, autoCreate); } }); } @Transactional public void schedule(final Queue queue, final Date scheduled, final List arguments) throws SQLException { jdbcTemplate.execute(new ConnectionCallback() { @Override public Object doInConnection(Connection c) throws SQLException, DataAccessException { queueService.schedule(c, queue, scheduled, arguments); return null; } }); } }