You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2012/01/18 18:35:38 UTC

svn commit: r1232965 - in /sling/trunk/bundles/commons/scheduler: pom.xml src/main/java/org/apache/sling/commons/scheduler/Scheduler.java src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java

Author: cziegeler
Date: Wed Jan 18 17:35:37 2012
New Revision: 1232965

URL: http://svn.apache.org/viewvc?rev=1232965&view=rev
Log:
SLING-2375 : Allow Immediate And Then Periodic Task Execution In Whiteboard Scheduling

Modified:
    sling/trunk/bundles/commons/scheduler/pom.xml
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java

Modified: sling/trunk/bundles/commons/scheduler/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/pom.xml?rev=1232965&r1=1232964&r2=1232965&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/pom.xml (original)
+++ sling/trunk/bundles/commons/scheduler/pom.xml Wed Jan 18 17:35:37 2012
@@ -58,7 +58,7 @@
                             *
                         </Import-Package>
                         <Export-Package>
-                            org.apache.sling.commons.scheduler;version=2.1.0
+                            org.apache.sling.commons.scheduler;version=2.2.0
                         </Export-Package>
                         <Private-Package>
                             org.apache.sling.commons.scheduler.impl

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java?rev=1232965&r1=1232964&r2=1232965&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java (original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/Scheduler.java Wed Jan 18 17:35:37 2012
@@ -30,9 +30,19 @@ import java.util.NoSuchElementException;
 public interface Scheduler {
 
     /** Name of the configuration property to define the period for a job.
-     * The period is expressed in seconds.*/
+     * The period is expressed in seconds.
+     * This property needs to be of type Long.
+     */
     String PROPERTY_SCHEDULER_PERIOD = "scheduler.period";
 
+    /** Name of the configuration property to define if a periodically job
+     * should be scheduled immediate.
+     * Default is to not startup immediate, the job is started the first time
+     * after the period has expired.
+     * This property needs to be of type Boolean.
+     * @since 2.2.0 .*/
+    String PROPERTY_SCHEDULER_IMMEDIATE = "scheduler.immediate";
+
     /** Name of the configuration property to define the cron expression for a job. */
     String PROPERTY_SCHEDULER_EXPRESSION = "scheduler.expression";
 
@@ -77,6 +87,25 @@ public interface Scheduler {
     throws Exception;
 
     /**
+     * Schedule a periodic job.
+     * Note that if a job with the same name has already been added, the old job is cancelled and this new job replaces
+     * the old job.
+     *
+     * @param name The name of the job - or null. If no name is specified it can't be cancelled.
+     * @param job The job to execute (either {@link Job} or {@link Runnable}).
+     * @param config An optional configuration object - this configuration is only passed to the job the job implements {@link Job}.
+     * @param period Every period seconds this job is started.
+     * @param canRunConcurrently Whether this job can run even if previous scheduled runs are still running.
+     * @param startImmediate Whether to start the job immediately for the first time or wait for the period to expire.
+     * @throws IllegalArgumentException If the job has not the correct type.
+     * @throws Exception If the job can't be scheduled.
+     * @since 2.2
+     */
+    void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently,
+            boolean startImmediate)
+    throws Exception;
+
+    /**
      * Fire a job immediately and only once.
      *
      * @param job The job to execute (either {@link Job} or {@link Runnable}).

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java?rev=1232965&r1=1232964&r2=1232965&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java (original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzScheduler.java Wed Jan 18 17:35:37 2012
@@ -47,6 +47,7 @@ import org.quartz.JobDetail;
 import org.quartz.JobKey;
 import org.quartz.SchedulerException;
 import org.quartz.SimpleScheduleBuilder;
+import org.quartz.SimpleTrigger;
 import org.quartz.Trigger;
 import org.quartz.TriggerBuilder;
 import org.quartz.impl.DirectSchedulerFactory;
@@ -181,7 +182,6 @@ public class QuartzScheduler implements 
         final String runID = new Date().toString().replace(' ', '_');
         factory.createScheduler(QUARTZ_SCHEDULER_NAME, runID, quartzPool, new RAMJobStore());
         // quartz does not provide a way to get the scheduler by name AND runID, so we have to iterate!
-        @SuppressWarnings("unchecked")
         final Iterator<org.quartz.Scheduler> allSchedulersIter = factory.getAllSchedulers().iterator();
         org.quartz.Scheduler s = null;
         while ( s == null && allSchedulersIter.hasNext() ) {
@@ -348,14 +348,32 @@ public class QuartzScheduler implements 
                                final long period,
                                final boolean canRunConcurrently)
     throws SchedulerException {
+        this.addPeriodicJob(name, job, config, period, canRunConcurrently, false);
+    }
+
+    /**
+     * @see org.apache.sling.commons.scheduler.Scheduler#addPeriodicJob(java.lang.String, java.lang.Object, java.util.Map, long, boolean, boolean)
+     */
+    public void addPeriodicJob(final String name,
+            final Object job,
+            final Map<String, Serializable> config,
+            final long period,
+            final boolean canRunConcurrently,
+            final boolean startImmediate)
+    throws SchedulerException {
         final long ms = period * 1000;
         final String jobName = this.getJobName(name);
 
-        final Trigger trigger = TriggerBuilder.newTrigger()
-            .withIdentity(jobName)
-            .startAt(new Date(System.currentTimeMillis() + ms))
-            .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInMilliseconds(ms))
-            .build();
+        final TriggerBuilder<SimpleTrigger> builder = TriggerBuilder.newTrigger()
+                .withIdentity(jobName)
+                .startAt(new Date(System.currentTimeMillis() + ms))
+                .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInMilliseconds(ms));
+        final Trigger trigger;
+        if ( startImmediate ) {
+            trigger = builder.startNow().build();
+        } else {
+            trigger = builder.startAt(new Date(System.currentTimeMillis() + ms)).build();
+        }
 
         this.scheduleJob(jobName, job, config, trigger, canRunConcurrently);
     }
@@ -524,7 +542,11 @@ public class QuartzScheduler implements 
                             if ( period < 1 ) {
                                 this.logger.debug("Ignoring service {} : scheduler period is less than 1.", ref);
                             } else {
-                                this.addPeriodicJob(name, job, null, period, (concurrent != null ? concurrent : true));
+                                boolean immediate = false;
+                                if ( ref.getProperty(Scheduler.PROPERTY_SCHEDULER_IMMEDIATE) != null ) {
+                                    immediate = (Boolean)ref.getProperty(Scheduler.PROPERTY_SCHEDULER_IMMEDIATE);
+                                }
+                                this.addPeriodicJob(name, job, null, period, (concurrent != null ? concurrent : true), immediate);
                             }
                         } else {
                             this.logger.debug("Ignoring servce {} : no scheduling property found.", ref);