From b59f08bb5b08be7972086037ec54b23ea9fb49f8 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 11 Apr 2013 19:12:27 +0200 Subject: wip --- src/main/java/io/trygvis/queue/AsyncService.java | 6 +- .../java/io/trygvis/queue/JpaAsyncService.java | 94 ++++++++++++---------- 2 files changed, 57 insertions(+), 43 deletions(-) (limited to 'src/main/java/io/trygvis/queue') 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