From b59f08bb5b08be7972086037ec54b23ea9fb49f8 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 11 Apr 2013 19:12:27 +0200 Subject: wip --- .../java/io/trygvis/CreateArticleCallable.java | 33 ++++++++ src/main/java/io/trygvis/Main.java | 12 ++- src/main/java/io/trygvis/MyJob.java | 38 --------- src/main/java/io/trygvis/UpdateArticeCallable.java | 43 ++++++++++ src/main/java/io/trygvis/queue/AsyncService.java | 6 +- .../java/io/trygvis/queue/JpaAsyncService.java | 94 ++++++++++++---------- src/main/java/io/trygvis/spring/Config.java | 5 -- 7 files changed, 142 insertions(+), 89 deletions(-) create mode 100755 src/main/java/io/trygvis/CreateArticleCallable.java delete mode 100755 src/main/java/io/trygvis/MyJob.java create mode 100755 src/main/java/io/trygvis/UpdateArticeCallable.java (limited to 'src/main/java') diff --git a/src/main/java/io/trygvis/CreateArticleCallable.java b/src/main/java/io/trygvis/CreateArticleCallable.java new file mode 100755 index 0000000..e727b46 --- /dev/null +++ b/src/main/java/io/trygvis/CreateArticleCallable.java @@ -0,0 +1,33 @@ +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 javax.persistence.*; + +import static org.springframework.transaction.annotation.Propagation.*; + +@Component +public class CreateArticleCallable implements AsyncService.AsyncCallable { + private final Logger log = LoggerFactory.getLogger(getClass()); + + @PersistenceContext + private EntityManager entityManager; + +// @Transactional(propagation = REQUIRES_NEW) + public void run() throws Exception { + log.info("CreateArticeJob.run: BEGIN"); + Date now = new Date(); + + log.info("now = {}", now); + + Article article = new Article(new Date(), null, "title", "body"); + entityManager.persist(article); + + log.info("CreateArticeJob.run: END"); + } +} diff --git a/src/main/java/io/trygvis/Main.java b/src/main/java/io/trygvis/Main.java index 19167d7..aafb728 100755 --- a/src/main/java/io/trygvis/Main.java +++ b/src/main/java/io/trygvis/Main.java @@ -29,7 +29,6 @@ public class Main { log.info("Starting context"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); - context.start(); log.info("Started context"); try { @@ -50,12 +49,19 @@ public class Main { @Autowired private AsyncService asyncService; + @Autowired + private CreateArticleCallable createArticleCallable; + + @Autowired + private UpdateArticeCallable updateArticeCallable; + public void run() throws Exception { log.info("Main.run"); - asyncService.registerQueue("test-queue", 1, MyJob.class); + asyncService.registerQueue("create-queue", 1, createArticleCallable); +// asyncService.registerQueue("update-queue", 1, updateArticeCallable); - AsyncService.QueueRef queue = asyncService.getQueue("test-queue"); + AsyncService.QueueRef queue = asyncService.getQueue("create-queue"); AsyncService.ExecutionRef executionRef = asyncService.schedule(queue); } diff --git a/src/main/java/io/trygvis/MyJob.java b/src/main/java/io/trygvis/MyJob.java deleted file mode 100755 index 7303a33..0000000 --- a/src/main/java/io/trygvis/MyJob.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.trygvis; - -import io.trygvis.model.*; -import org.quartz.*; -import org.slf4j.*; - -import java.util.*; -import javax.persistence.*; - -public class MyJob implements Job { - private final Logger log = LoggerFactory.getLogger(getClass()); - - @PersistenceContext - private EntityManager entityManager; - - public void execute(JobExecutionContext context) throws JobExecutionException { - log.info("MyJob.execute: BEGIN"); - log.info("entityManager = {}", entityManager); - log.info("context.getJobDetail().getKey() = {}", context.getJobDetail().getKey()); - -/* - Date now = new Date(); - - log.info("now = {}", now); - -// Article article = entityManager.find(Article.class, 1); -// -// System.out.println("article = " + article); -// article.setUpdated(now); -// entityManager.persist(article); - - Article article = new Article(new Date(), null, "title", "body"); - entityManager.persist(article); -*/ - - log.info("MyJob.execute: END"); - } -} diff --git a/src/main/java/io/trygvis/UpdateArticeCallable.java b/src/main/java/io/trygvis/UpdateArticeCallable.java new file mode 100755 index 0000000..d022655 --- /dev/null +++ b/src/main/java/io/trygvis/UpdateArticeCallable.java @@ -0,0 +1,43 @@ +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 javax.persistence.*; + +import static org.springframework.transaction.annotation.Propagation.*; + +@Component +public class UpdateArticeCallable implements AsyncService.AsyncCallable { + private final Logger log = LoggerFactory.getLogger(getClass()); + + private final Random r = new Random(); + + @PersistenceContext + private EntityManager entityManager; + +// @Transactional(propagation = REQUIRES_NEW) + public void run() throws Exception { + log.info("UpdateArticeJob.run: BEGIN"); + + Date now = new Date(); + + log.info("now = {}", now); + + TypedQuery
q = entityManager.createQuery(entityManager.getCriteriaBuilder().createQuery(Article.class)); + + List
list = q.getResultList(); + log.info("Got {} articles", list.size()); + + Article a = list.get(r.nextInt(list.size())); + a.setUpdated(new Date()); + + entityManager.persist(a); + + log.info("UpdateArticeJob.run: END"); + } +} diff --git a/src/main/java/io/trygvis/queue/AsyncService.java b/src/main/java/io/trygvis/queue/AsyncService.java index dcbe991..de0a1af 100755 --- a/src/main/java/io/trygvis/queue/AsyncService.java +++ b/src/main/java/io/trygvis/queue/AsyncService.java @@ -4,7 +4,7 @@ import org.quartz.*; public interface AsyncService { - void registerQueue(String name, int interval, Class klass) throws SchedulerException; + void registerQueue(String name, int interval, AsyncCallable callable) throws SchedulerException; QueueRef getQueue(String name); @@ -15,4 +15,8 @@ public interface AsyncService { +public class JpaAsyncService implements AsyncService, + ApplicationContextAware { private final Logger log = LoggerFactory.getLogger(getClass()); @@ -64,13 +54,9 @@ public class JpaAsyncService implements AsyncService callables = new ArrayList<>(); + } + + private final Context context = new Context(); + + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.context.applicationContext = applicationContext; + } + @PostConstruct - public void afterPropertiesSet() throws Exception { + public void postConstruct() throws Exception { transactionManagerStatic = transactionManager; dataSourceStatic = dataSource; log.info("afterPropertiesSet!!"); @@ -91,18 +92,24 @@ public class JpaAsyncService implements AsyncService entry : jobExecutionContext.getMergedJobDataMap().entrySet()) { + log.info("{} = {}, class= {}", entry.getKey(), entry.getValue(), entry.getValue() != null ? entry.getValue().getClass() : ""); + } - log.info("className = {}", className); + int index = jobExecutionContext.getMergedJobDataMap().getIntFromString("index"); + AsyncCallable callable = context.callables.get(index); - Class klass = getClass().getClassLoader().loadClass(className); - Object bean = applicationContext.getBean(klass); + log.info("Calling {}", callable); + callable.run(); - log.info("bean = {}", bean); - } catch (Exception e) { + } catch (Throwable e) { log.warn("fail", e); - throw new JobExecutionException(e, false); +// throw new JobExecutionException(e, false); } } } @@ -218,14 +228,14 @@ public class JpaAsyncService implements AsyncService