From 74a3da1853eeb2e853d17d8d8527404248b209d3 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 20 Apr 2013 18:10:20 +0200 Subject: wip --- .../java/io/trygvis/CreateArticleCallable.java | 21 ++++---- src/main/java/io/trygvis/Main.java | 30 ++++++----- src/main/java/io/trygvis/data/QueueRepository.java | 4 +- src/main/java/io/trygvis/data/TaskRepository.java | 6 +-- src/main/java/io/trygvis/model/Article.java | 4 +- src/main/java/io/trygvis/queue/AsyncService.java | 2 +- .../java/io/trygvis/queue/JdbcAsyncService.java | 60 ++++++++++++++-------- src/main/java/io/trygvis/queue/QueueDao.java | 12 +++-- src/main/java/io/trygvis/queue/Task.java | 5 +- src/main/java/io/trygvis/queue/TaskDao.java | 17 +++--- src/main/java/io/trygvis/spring/Config.java | 2 - 11 files changed, 96 insertions(+), 67 deletions(-) (limited to 'src/main') diff --git a/src/main/java/io/trygvis/CreateArticleCallable.java b/src/main/java/io/trygvis/CreateArticleCallable.java index 671f3dc..420a5b5 100755 --- a/src/main/java/io/trygvis/CreateArticleCallable.java +++ b/src/main/java/io/trygvis/CreateArticleCallable.java @@ -1,14 +1,17 @@ package io.trygvis; -import io.trygvis.model.*; -import io.trygvis.queue.*; -import org.slf4j.*; -import org.springframework.stereotype.*; -import org.springframework.transaction.annotation.*; - -import java.util.*; - -import static org.springframework.transaction.annotation.Propagation.*; +import io.trygvis.model.Article; +import io.trygvis.queue.AsyncService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; +import java.util.Random; + +import static org.springframework.transaction.annotation.Propagation.MANDATORY; @Component("createArticle") @Transactional(propagation = MANDATORY) diff --git a/src/main/java/io/trygvis/Main.java b/src/main/java/io/trygvis/Main.java index f1bba26..d22cab2 100755 --- a/src/main/java/io/trygvis/Main.java +++ b/src/main/java/io/trygvis/Main.java @@ -1,20 +1,26 @@ package io.trygvis; -import io.trygvis.queue.*; +import io.trygvis.queue.AsyncService; import io.trygvis.queue.Queue; -import org.hibernate.dialect.*; -import org.slf4j.*; -import org.slf4j.bridge.*; -import org.springframework.beans.factory.annotation.*; -import org.springframework.context.support.*; -import org.springframework.stereotype.*; -import org.springframework.transaction.*; -import org.springframework.transaction.support.*; - -import java.util.*; +import io.trygvis.queue.Task; +import org.hibernate.dialect.PostgreSQL82Dialect; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.bridge.SLF4JBridgeHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.stereotype.Component; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.TransactionCallbackWithoutResult; +import org.springframework.transaction.support.TransactionTemplate; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import static java.lang.System.*; -import static java.lang.Thread.*; +import static java.lang.Thread.sleep; @Component public class Main { diff --git a/src/main/java/io/trygvis/data/QueueRepository.java b/src/main/java/io/trygvis/data/QueueRepository.java index 604d5fe..4ea6707 100755 --- a/src/main/java/io/trygvis/data/QueueRepository.java +++ b/src/main/java/io/trygvis/data/QueueRepository.java @@ -1,7 +1,7 @@ package io.trygvis.data; -import io.trygvis.queue.*; -import org.springframework.data.jpa.repository.*; +import io.trygvis.queue.Queue; +import org.springframework.data.jpa.repository.JpaRepository; public interface QueueRepository extends JpaRepository { Queue findByName(String name); diff --git a/src/main/java/io/trygvis/data/TaskRepository.java b/src/main/java/io/trygvis/data/TaskRepository.java index efe4903..c0df329 100755 --- a/src/main/java/io/trygvis/data/TaskRepository.java +++ b/src/main/java/io/trygvis/data/TaskRepository.java @@ -1,10 +1,10 @@ package io.trygvis.data; -import io.trygvis.queue.*; import io.trygvis.queue.Queue; -import org.springframework.data.jpa.repository.*; +import io.trygvis.queue.Task; +import org.springframework.data.jpa.repository.JpaRepository; -import java.util.*; +import java.util.List; public interface TaskRepository extends JpaRepository { List findByQueueAndCompletedIsNull(Queue queue); diff --git a/src/main/java/io/trygvis/model/Article.java b/src/main/java/io/trygvis/model/Article.java index d144217..e86c570 100755 --- a/src/main/java/io/trygvis/model/Article.java +++ b/src/main/java/io/trygvis/model/Article.java @@ -1,13 +1,11 @@ package io.trygvis.model; -import java.util.Date; - import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; -import javax.persistence.Table; +import java.util.Date; @Entity public class Article { diff --git a/src/main/java/io/trygvis/queue/AsyncService.java b/src/main/java/io/trygvis/queue/AsyncService.java index b42b550..41bb75a 100755 --- a/src/main/java/io/trygvis/queue/AsyncService.java +++ b/src/main/java/io/trygvis/queue/AsyncService.java @@ -1,6 +1,6 @@ package io.trygvis.queue; -import org.quartz.*; +import org.quartz.SchedulerException; import java.util.List; diff --git a/src/main/java/io/trygvis/queue/JdbcAsyncService.java b/src/main/java/io/trygvis/queue/JdbcAsyncService.java index a8f581e..06e7eee 100644 --- a/src/main/java/io/trygvis/queue/JdbcAsyncService.java +++ b/src/main/java/io/trygvis/queue/JdbcAsyncService.java @@ -1,19 +1,27 @@ package io.trygvis.queue; -import org.quartz.*; -import org.slf4j.*; -import org.springframework.beans.factory.annotation.*; -import org.springframework.stereotype.*; -import org.springframework.transaction.annotation.*; -import org.springframework.transaction.support.*; - -import java.util.*; -import java.util.concurrent.*; - -import static java.util.Arrays.*; -import static java.util.concurrent.TimeUnit.*; -import static org.springframework.transaction.annotation.Propagation.*; -import static org.springframework.transaction.support.TransactionSynchronizationManager.*; +import org.quartz.SchedulerException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.TransactionSynchronization; +import org.springframework.transaction.support.TransactionSynchronizationAdapter; +import org.springframework.transaction.support.TransactionTemplate; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledThreadPoolExecutor; + +import static java.lang.System.currentTimeMillis; +import static java.lang.Thread.sleep; +import static java.util.Arrays.asList; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.springframework.transaction.annotation.Propagation.REQUIRED; +import static org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization; @Component public class JdbcAsyncService implements AsyncService { @@ -96,12 +104,6 @@ public class JdbcAsyncService implements AsyncService { long id = taskDao.insert(queue.name, scheduled, arguments.toString()); Task task = new Task(id, queue.name, scheduled, null, 0, null, asList(args)); log.info("Created task = {}", task); -// queues.get(queue.name).ping(); -// try { -// Thread.sleep(500); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } registerSynchronization(new TransactionSynchronizationAdapter() { public void afterCompletion(int status) { @@ -114,6 +116,24 @@ public class JdbcAsyncService implements AsyncService { return task; } + @Transactional + public Task await(Task task, long timeout) { + final long start = currentTimeMillis(); + final long end = start + timeout; + + while (currentTimeMillis() < end) { + task = update(task); + + try { + sleep(100); + } catch (InterruptedException e) { + // break + } + } + + return task; + } + @Transactional(readOnly = true) public Task update(Task ref) { return taskDao.findById(ref.id); diff --git a/src/main/java/io/trygvis/queue/QueueDao.java b/src/main/java/io/trygvis/queue/QueueDao.java index a3a79ca..63dde2a 100644 --- a/src/main/java/io/trygvis/queue/QueueDao.java +++ b/src/main/java/io/trygvis/queue/QueueDao.java @@ -1,12 +1,14 @@ package io.trygvis.queue; -import org.springframework.beans.factory.annotation.*; -import org.springframework.jdbc.core.*; -import org.springframework.stereotype.*; +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.*; +import java.sql.ResultSet; +import java.sql.SQLException; -import static org.springframework.dao.support.DataAccessUtils.*; +import static org.springframework.dao.support.DataAccessUtils.singleResult; @Component public class QueueDao { diff --git a/src/main/java/io/trygvis/queue/Task.java b/src/main/java/io/trygvis/queue/Task.java index 7f64c77..9a2e65b 100755 --- a/src/main/java/io/trygvis/queue/Task.java +++ b/src/main/java/io/trygvis/queue/Task.java @@ -1,6 +1,7 @@ package io.trygvis.queue; -import java.util.*; +import java.util.Date; +import java.util.List; public class Task { @@ -34,7 +35,7 @@ public class Task { } public Task registerComplete(Date completed) { - return new Task(id, queue, scheduled, lastRun, runCount, new Date(), arguments); + return new Task(id, queue, scheduled, lastRun, runCount, completed, arguments); } public String toString() { diff --git a/src/main/java/io/trygvis/queue/TaskDao.java b/src/main/java/io/trygvis/queue/TaskDao.java index 2bf2145..dac99c7 100644 --- a/src/main/java/io/trygvis/queue/TaskDao.java +++ b/src/main/java/io/trygvis/queue/TaskDao.java @@ -1,18 +1,19 @@ package io.trygvis.queue; -import org.springframework.beans.factory.annotation.*; -import org.springframework.jdbc.core.*; -import org.springframework.stereotype.*; -import org.springframework.transaction.annotation.Propagation; +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.*; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collections; import java.util.Date; -import java.util.*; +import java.util.List; -import static java.util.Arrays.*; +import static java.util.Arrays.asList; import static org.springframework.transaction.annotation.Propagation.MANDATORY; -import static org.springframework.transaction.annotation.Propagation.REQUIRED; @Component public class TaskDao { diff --git a/src/main/java/io/trygvis/spring/Config.java b/src/main/java/io/trygvis/spring/Config.java index ca75049..df4b2e2 100755 --- a/src/main/java/io/trygvis/spring/Config.java +++ b/src/main/java/io/trygvis/spring/Config.java @@ -10,12 +10,10 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; -import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.support.TransactionTemplate; -import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; @Configuration -- cgit v1.2.3