deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject git commit: DELTASPIKE-542 support for persistent scheduler-jobs
Date Wed, 19 Mar 2014 20:29:26 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 57c621a21 -> 61c68f012


DELTASPIKE-542 support for persistent scheduler-jobs


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/61c68f01
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/61c68f01
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/61c68f01

Branch: refs/heads/master
Commit: 61c68f012bb3799a235d6273e5f77b7fc9d45460
Parents: 57c621a
Author: gpetracek <gpetracek@apache.org>
Authored: Tue Mar 18 15:45:00 2014 +0100
Committer: gpetracek <gpetracek@apache.org>
Committed: Sun Mar 16 17:55:07 2014 +0100

----------------------------------------------------------------------
 .../deltaspike/scheduler/api/Scheduled.java     |  2 +
 .../scheduler/impl/QuartzScheduler.java         | 61 +++++++++++++++++---
 2 files changed, 54 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/61c68f01/deltaspike/modules/scheduler/api/src/main/java/org/apache/deltaspike/scheduler/api/Scheduled.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/scheduler/api/src/main/java/org/apache/deltaspike/scheduler/api/Scheduled.java
b/deltaspike/modules/scheduler/api/src/main/java/org/apache/deltaspike/scheduler/api/Scheduled.java
index 6732e5a..587b615 100644
--- a/deltaspike/modules/scheduler/api/src/main/java/org/apache/deltaspike/scheduler/api/Scheduled.java
+++ b/deltaspike/modules/scheduler/api/src/main/java/org/apache/deltaspike/scheduler/api/Scheduled.java
@@ -35,6 +35,8 @@ public @interface Scheduled
 {
     String cronExpression();
 
+    boolean overrideOnStartup() default false; //'true' triggers a re-schedule if the job
exists already
+
     Class<? extends Annotation>[] startScopes() default { SessionScoped.class, RequestScoped.class
};
 
     Class group() default Scheduled.class; //type-safe group

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/61c68f01/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 9eb1be8..4b83f67 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
@@ -40,12 +40,14 @@ import org.quartz.impl.StdSchedulerFactory;
 
 import java.lang.annotation.Annotation;
 import java.util.Collections;
+import java.util.List;
 import java.util.Stack;
+import java.util.logging.Logger;
 
 //vetoed class (see SchedulerExtension)
 public class QuartzScheduler implements Scheduler<Job>
 {
-    private org.quartz.Scheduler scheduler;
+    protected org.quartz.Scheduler scheduler;
 
     @Override
     public void start()
@@ -120,15 +122,56 @@ public class QuartzScheduler implements Scheduler<Job>
                 description = jobClass.getName();
             }
 
-            JobDetail jobDetail = JobBuilder.newJob(jobClass)
-                    .withDescription(description)
-                    .withIdentity(jobKey)
-                    .build();
-            Trigger trigger = TriggerBuilder.newTrigger()
-                    .withSchedule(CronScheduleBuilder.cronSchedule(scheduled.cronExpression()))
-                    .build();
+            JobDetail jobDetail = this.scheduler.getJobDetail(jobKey);
+            Trigger trigger;
+
+            if (jobDetail == null)
+            {
+                jobDetail = JobBuilder.newJob(jobClass)
+                        .withDescription(description)
+                        .withIdentity(jobKey)
+                        .build();
+
+                trigger = TriggerBuilder.newTrigger()
+                        .withSchedule(CronScheduleBuilder.cronSchedule(scheduled.cronExpression()))
+                        .build();
+
+                this.scheduler.scheduleJob(jobDetail, trigger);
+            }
+            else if (scheduled.overrideOnStartup())
+            {
+                List<? extends Trigger> existingTriggers = this.scheduler.getTriggersOfJob(jobKey);
+
+                if (existingTriggers == null || existingTriggers.isEmpty())
+                {
+                    //TODO re-visit it
+                    trigger = TriggerBuilder.newTrigger()
+                            .withSchedule(CronScheduleBuilder.cronSchedule(scheduled.cronExpression()))
+                            .build();
 
-            this.scheduler.scheduleJob(jobDetail, trigger);
+                    this.scheduler.scheduleJob(jobDetail, trigger);
+                    return;
+                }
+
+                if (existingTriggers.size() > 1)
+                {
+                    throw new IllegalStateException("multiple triggers found for " + jobKey
+ " ('" + jobDetail + "')" +
+                        ", but aren't supported by @" + Scheduled.class.getName() + "#overrideOnStartup");
+                }
+
+                trigger = existingTriggers.iterator().next();
+
+                trigger = TriggerBuilder.newTrigger()
+                        .withIdentity(trigger.getKey())
+                        .withSchedule(CronScheduleBuilder.cronSchedule(scheduled.cronExpression()))
+                        .build();
+
+                this.scheduler.rescheduleJob(trigger.getKey(), trigger);
+            }
+            else
+            {
+                Logger.getLogger(QuartzScheduler.class.getName()).info(jobKey + " exists
already and will be ignored.");
+            }
         }
         catch (SchedulerException e)
         {


Mime
View raw message