You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2014/06/10 16:22:48 UTC
git commit: DELTASPIKE-632 CdiAwareJobFactory
Repository: deltaspike
Updated Branches:
refs/heads/master 7eeb21102 -> c62ce6cf3
DELTASPIKE-632 CdiAwareJobFactory
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/c62ce6cf
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/c62ce6cf
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/c62ce6cf
Branch: refs/heads/master
Commit: c62ce6cf34539b864eed4118bb67f75a945cd2bf
Parents: 7eeb211
Author: gpetracek <gp...@apache.org>
Authored: Tue Jun 10 16:12:28 2014 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Tue Jun 10 16:16:40 2014 +0200
----------------------------------------------------------------------
.../scheduler/impl/CdiAwareJobFactory.java | 62 ++++++++++++++
.../scheduler/impl/QuartzScheduler.java | 86 ++++++++++++++++++--
2 files changed, 141 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/c62ce6cf/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/CdiAwareJobFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/CdiAwareJobFactory.java b/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/CdiAwareJobFactory.java
new file mode 100644
index 0000000..9268a76
--- /dev/null
+++ b/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/CdiAwareJobFactory.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.scheduler.impl;
+
+import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.util.ClassUtils;
+import org.quartz.Job;
+import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
+import org.quartz.spi.JobFactory;
+import org.quartz.spi.TriggerFiredBundle;
+
+public class CdiAwareJobFactory implements JobFactory
+{
+ private final JobFactory defaultFactory;
+
+ public CdiAwareJobFactory()
+ {
+ String defaultJobFactoryName =
+ ConfigResolver.getPropertyValue("deltaspike.scheduler.DefaultJobFactory",
+ "org.quartz.simpl.PropertySettingJobFactory");
+
+ defaultFactory = ClassUtils.tryToInstantiateClassForName(defaultJobFactoryName, JobFactory.class);
+ }
+
+ @Override
+ public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException
+ {
+ Job result = null;
+ try
+ {
+ Class<? extends Job> jobClass = bundle.getJobDetail().getJobClass();
+ result = BeanProvider.getContextualReference(jobClass);
+ scheduler.getContext().put(jobClass.getName(), Boolean.TRUE);
+ }
+ catch (Exception e)
+ {
+ if (result == null)
+ {
+ result = defaultFactory.newJob(bundle, scheduler);
+ }
+ }
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/c62ce6cf/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/QuartzScheduler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/QuartzScheduler.java b/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/QuartzScheduler.java
index 86e7046..9350cf1 100644
--- a/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/QuartzScheduler.java
+++ b/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/QuartzScheduler.java
@@ -21,7 +21,10 @@ package org.apache.deltaspike.scheduler.impl;
import org.apache.deltaspike.cdise.api.ContextControl;
import org.apache.deltaspike.core.api.config.ConfigResolver;
import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.util.ClassUtils;
import org.apache.deltaspike.core.util.ExceptionUtils;
+import org.apache.deltaspike.core.util.PropertyFileUtils;
+import org.apache.deltaspike.core.util.ProxyUtils;
import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider;
import org.apache.deltaspike.scheduler.api.Scheduled;
import org.apache.deltaspike.scheduler.spi.Scheduler;
@@ -39,15 +42,21 @@ import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
+import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.List;
+import java.util.Properties;
+import java.util.ResourceBundle;
import java.util.Stack;
+import java.util.logging.Level;
import java.util.logging.Logger;
//vetoed class (see SchedulerExtension)
public class QuartzScheduler implements Scheduler<Job>
{
+ private static final Logger LOG = Logger.getLogger(QuartzScheduler.class.getName());
private static final Scheduled DEFAULT_SCHEDULED_LITERAL = AnnotationInstanceProvider.of(Scheduled.class);
protected org.quartz.Scheduler scheduler;
@@ -60,15 +69,55 @@ public class QuartzScheduler implements Scheduler<Job>
throw new UnsupportedOperationException("the scheduler is started already");
}
- SchedulerFactory schedulerFactory;
+ SchedulerFactory schedulerFactory = null;
try
{
- String configFile =
- ConfigResolver.getPropertyValue("deltaspike.scheduler.quartz_config-file", "quartz.properties");
- schedulerFactory = new StdSchedulerFactory(configFile);
+ Properties properties = new Properties();
+ properties.put(StdSchedulerFactory.PROP_SCHED_JOB_FACTORY_CLASS, CdiAwareJobFactory.class.getName());
+
+ try
+ {
+ ResourceBundle config = loadCustomQuartzConfig();
+
+ Enumeration<String> keys = config.getKeys();
+ String key;
+ while (keys.hasMoreElements())
+ {
+ key = keys.nextElement();
+ properties.put(key, config.getString(key));
+ }
+ }
+ catch (Exception e1)
+ {
+ LOG.info("no custom quartz-config file found. falling back to the default config provided by quartz.");
+
+ InputStream inputStream = null;
+ try
+ {
+ inputStream = ClassUtils.getClassLoader(null).getResourceAsStream("org/quartz/quartz.properties");
+ properties.load(inputStream);
+ }
+ catch (Exception e2)
+ {
+ LOG.warning("failed to load quartz default-config");
+ schedulerFactory = new StdSchedulerFactory();
+ }
+ finally
+ {
+ if (inputStream != null)
+ {
+ inputStream.close();
+ }
+ }
+ }
+ if (schedulerFactory == null)
+ {
+ schedulerFactory = new StdSchedulerFactory(properties);
+ }
}
- catch (SchedulerException e)
+ catch (Exception e)
{
+ LOG.log(Level.WARNING, "fallback to default scheduler-factory", e);
schedulerFactory = new StdSchedulerFactory();
}
@@ -90,6 +139,13 @@ public class QuartzScheduler implements Scheduler<Job>
}
}
+ protected ResourceBundle loadCustomQuartzConfig()
+ {
+ String configFile =
+ ConfigResolver.getPropertyValue("deltaspike.scheduler.quartz_config-file", "quartz.properties");
+ return PropertyFileUtils.getResourceBundle(configFile);
+ }
+
@Override
public void stop()
{
@@ -296,7 +352,8 @@ public class QuartzScheduler implements Scheduler<Job>
@Override
public void jobToBeExecuted(JobExecutionContext jobExecutionContext)
{
- Scheduled scheduled = jobExecutionContext.getJobInstance().getClass().getAnnotation(Scheduled.class);
+ Class<?> jobClass = ProxyUtils.getUnproxiedClass(jobExecutionContext.getJobInstance().getClass());
+ Scheduled scheduled = jobClass.getAnnotation(Scheduled.class);
//can happen with manually registered job-instances (via #unwrap)
if (scheduled == null)
@@ -316,7 +373,22 @@ public class QuartzScheduler implements Scheduler<Job>
}
}
- BeanProvider.injectFields(jobExecutionContext.getJobInstance());
+ boolean jobInstanceIsBean;
+
+ try
+ {
+ jobInstanceIsBean =
+ Boolean.TRUE.equals(jobExecutionContext.getScheduler().getContext().get(jobClass.getName()));
+ }
+ catch (SchedulerException e)
+ {
+ jobInstanceIsBean = false;
+ }
+
+ if (!jobInstanceIsBean)
+ {
+ BeanProvider.injectFields(jobExecutionContext.getJobInstance());
+ }
}
@Override