incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r818017 - in /sling/trunk/bundles/commons/scheduler: ./ src/main/java/org/apache/sling/commons/scheduler/ src/main/java/org/apache/sling/commons/scheduler/impl/
Date Wed, 23 Sep 2009 09:35:05 GMT
Author: cziegeler
Date: Wed Sep 23 09:35:04 2009
New Revision: 818017

URL: http://svn.apache.org/viewvc?rev=818017&view=rev
Log:
SLING-447 - Add support to fire a job n times; as the api is enhanced, the version is updated
to 2.1.0

Added:
    sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
  (with props)
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/QuartzJobExecutor.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=818017&r1=818016&r2=818017&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/pom.xml (original)
+++ sling/trunk/bundles/commons/scheduler/pom.xml Wed Sep 23 09:35:04 2009
@@ -29,7 +29,7 @@
 
     <artifactId>org.apache.sling.commons.scheduler</artifactId>
     <packaging>bundle</packaging>
-    <version>2.0.5-SNAPSHOT</version>
+    <version>2.1.0-SNAPSHOT</version>
 
     <name>Apache Sling Scheduler Support</name>
     <description>

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=818017&r1=818016&r2=818017&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 Sep 23 09:35:04 2009
@@ -88,6 +88,19 @@
     throws Exception;
 
     /**
+     * Fire a job immediately more than once.
+     *
+     * @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 times The number of times this job should be started (must be higher than 1)
+     * @param period Every period seconds this job is started.
+     * @throws IllegalArgumentException If the job has not the correct type.
+     * @return true if the code could be added, false otherwise.
+     * @since 2.1
+     */
+    boolean fireJob(Object job, Map<String, Serializable> config, int times, long period);
+
+    /**
      * Fire a job once at a specific date
      * Note that if a job with the same name has already beed added, the old job is cancelled
and this new job replaces
      * the old job.
@@ -103,6 +116,23 @@
     throws Exception;
 
     /**
+     * Fire a job once at a specific date, several times with a given interval.
+     * Note that if a job with the same name has already beed 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 date The date this job should be run.
+     * @param times The number of times this job should be started (must be higher than 1)
+     * @param period Every period seconds this job is started.
+     * @throws IllegalArgumentException If the job has not the correct type.
+     * @return true if the code could be added, false otherwise.
+     * @since 2.1
+     */
+    boolean fireJobAt(String name, Object job, Map<String, Serializable> config, Date
date, int times, long period);
+
+    /**
      * Remove a scheduled job by name.
      *
      * @param name The name of the job.

Added: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java?rev=818017&view=auto
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
(added)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
Wed Sep 23 09:35:04 2009
@@ -0,0 +1,34 @@
+/*
+ * 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.sling.commons.scheduler.impl;
+
+/**
+ * The job handler controls the invocation of a job
+ * like parallel invocation and the number of times
+ * the job can be called.
+ */
+public class JobHandler {
+
+    public final boolean runConcurrently;
+
+    public volatile boolean isRunning;
+
+    public JobHandler(final boolean runConcurrently) {
+        this.runConcurrently = runConcurrently;
+        this.isRunning = false;
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/JobHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java?rev=818017&r1=818016&r2=818017&view=diff
==============================================================================
--- sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
(original)
+++ sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/QuartzJobExecutor.java
Wed Sep 23 09:35:04 2009
@@ -40,15 +40,14 @@
 
         final JobDataMap data = context.getJobDetail().getJobDataMap();
 
-        final QuartzScheduler.ConcurrentHandler concurrentHandler
-             = (QuartzScheduler.ConcurrentHandler)data.get(QuartzScheduler.DATA_MAP_CONCURRENT_HANDLER);
-        final boolean canRunConcurrently = (concurrentHandler == null ? true : concurrentHandler.runConcurrently);
+        final JobHandler handler = (JobHandler)data.get(QuartzScheduler.DATA_MAP_JOB_HANDLER);
+        final boolean canRunConcurrently = (handler == null ? true : handler.runConcurrently);
 
         if (!canRunConcurrently) {
-            if ( concurrentHandler.isRunning ) {
+            if ( handler.isRunning ) {
                 return;
             }
-            concurrentHandler.isRunning = true;
+            handler.isRunning = true;
         }
 
         final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
@@ -77,7 +76,7 @@
             logger.error("Exception during job execution of " + job + " : " + t.getMessage(),
t);
         } finally {
             if (!canRunConcurrently) {
-                concurrentHandler.isRunning = false;
+                handler.isRunning = false;
             }
         }
     }

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=818017&r1=818016&r2=818017&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 Sep 23 09:35:04 2009
@@ -79,8 +79,8 @@
     /** Map key for the logger. */
     static final String DATA_MAP_LOGGER = "QuartzJobScheduler.Logger";
 
-    /** Map key for the concurrent handler */
-    static final String DATA_MAP_CONCURRENT_HANDLER = "QuartzJobExecutor.ConcurrentHandler";
+    /** Map key for the job handler */
+    static final String DATA_MAP_JOB_HANDLER = "QuartzJobExecutor.JobHandler";
 
     /** Theq quartz scheduler. */
     protected volatile org.quartz.Scheduler scheduler;
@@ -203,7 +203,7 @@
      *
      * @throws SchedulerException thrown in case of errors
      */
-    protected void scheduleJob(String name,
+    protected void scheduleJob(final String name,
                                final Object job,
                                final Map<String, Serializable>    config,
                                final Trigger trigger,
@@ -228,16 +228,16 @@
                 }
             } catch (final SchedulerException ignored) {
             }
-        } else {
-            name = PREFIX + UUID.randomUUID().toString();
         }
 
+        final String jobName = this.getJobName(name);
+
         // create the data map
-        final JobDataMap jobDataMap = this.initDataMap(name, job, config, canRunConcurrently);
+        final JobDataMap jobDataMap = this.initDataMap(jobName, job, config, canRunConcurrently);
 
-        final JobDetail detail = this.createJobDetail(name, jobDataMap);
+        final JobDetail detail = this.createJobDetail(jobName, jobDataMap);
 
-        this.logger.debug("Scheduling job {} with name {} and trigger {}", new Object[] {job,
name, trigger});
+        this.logger.debug("Scheduling job {} with name {} and trigger {}", new Object[] {job,
jobName, trigger});
         s.scheduleJob(detail, trigger);
     }
 
@@ -249,18 +249,17 @@
      * @param concurent
      * @return
      */
-    protected JobDataMap initDataMap(String  jobName,
-                                     Object  job,
-                                     Map<String, Serializable> config,
-                                     boolean concurent) {
+    protected JobDataMap initDataMap(final String  jobName,
+                                     final Object  job,
+                                     final Map<String, Serializable> config,
+                                     final boolean concurrent) {
         final JobDataMap jobDataMap = new JobDataMap();
 
         jobDataMap.put(DATA_MAP_OBJECT, job);
 
         jobDataMap.put(DATA_MAP_NAME, jobName);
-        final ConcurrentHandler handler = new ConcurrentHandler();
-        handler.runConcurrently = concurent;
-        jobDataMap.put(DATA_MAP_CONCURRENT_HANDLER, handler);
+        final JobHandler handler = new JobHandler(concurrent);
+        jobDataMap.put(DATA_MAP_JOB_HANDLER, handler);
         jobDataMap.put(DATA_MAP_LOGGER, this.logger);
         if ( config != null ) {
             jobDataMap.put(DATA_MAP_CONFIGURATION, config);
@@ -275,7 +274,7 @@
      * @param jobDataMap
      * @return
      */
-    protected JobDetail createJobDetail(String name, JobDataMap jobDataMap) {
+    protected JobDetail createJobDetail(final String name, final JobDataMap jobDataMap) {
         final JobDetail detail = new JobDetail(name, DEFAULT_QUARTZ_JOB_GROUP, QuartzJobExecutor.class);
         detail.setJobDataMap(jobDataMap);
         return detail;
@@ -294,11 +293,11 @@
     /**
      * @see org.apache.sling.commons.scheduler.Scheduler#addJob(java.lang.String, java.lang.Object,
java.util.Map, java.lang.String, boolean)
      */
-    public void addJob(String name,
-                       Object job,
-                       Map<String, Serializable>    config,
-                       String schedulingExpression,
-                       boolean canRunConcurrently)
+    public void addJob(final String name,
+                       final Object job,
+                       final Map<String, Serializable>    config,
+                       final String schedulingExpression,
+                       final boolean canRunConcurrently)
     throws SchedulerException {
         final CronTrigger cronJobEntry = new CronTrigger(name, DEFAULT_QUARTZ_JOB_GROUP);
 
@@ -313,44 +312,105 @@
     /**
      * @see org.apache.sling.commons.scheduler.Scheduler#addPeriodicJob(java.lang.String,
java.lang.Object, java.util.Map, long, boolean)
      */
-    public void addPeriodicJob(String name, Object job, Map<String, Serializable> config,
long period, boolean canRunConcurrently)
+    public void addPeriodicJob(final String name,
+                               final Object job,
+                               final Map<String, Serializable> config,
+                               final long period,
+                               final boolean canRunConcurrently)
     throws SchedulerException {
         final long ms = period * 1000;
-        if ( name == null ) {
-            name = PREFIX + UUID.randomUUID().toString();
-        }
+        final String jobName = this.getJobName(name);
+
         final SimpleTrigger timeEntry =
-            new SimpleTrigger(name, DEFAULT_QUARTZ_JOB_GROUP, new Date(System.currentTimeMillis()
+ ms), null,
+            new SimpleTrigger(jobName, DEFAULT_QUARTZ_JOB_GROUP, new Date(System.currentTimeMillis()
+ ms), null,
                               SimpleTrigger.REPEAT_INDEFINITELY, ms);
 
-        this.scheduleJob(name, job, config, timeEntry, canRunConcurrently);
+        this.scheduleJob(jobName, job, config, timeEntry, canRunConcurrently);
     }
 
     /**
      * @see org.apache.sling.commons.scheduler.Scheduler#fireJob(java.lang.Object, java.util.Map)
      */
-    public void fireJob(Object job, Map<String, Serializable> config)
+    public void fireJob(final Object job, final Map<String, Serializable> config)
     throws SchedulerException {
         this.checkJob(job);
-        final String name = job.getClass().getName();
-        final JobDataMap dataMap = this.initDataMap(name, job, config, true);
+        final String jobName = job.getClass().getName();
+        final JobDataMap dataMap = this.initDataMap(jobName, job, config, true);
 
-        final JobDetail detail = this.createJobDetail(name, dataMap);
+        final JobDetail detail = this.createJobDetail(jobName, dataMap);
 
-        final Trigger trigger = new SimpleTrigger(name, DEFAULT_QUARTZ_JOB_GROUP);
+        final Trigger trigger = new SimpleTrigger(jobName, DEFAULT_QUARTZ_JOB_GROUP);
         this.scheduler.scheduleJob(detail, trigger);
     }
 
     /**
      * @see org.apache.sling.commons.scheduler.Scheduler#fireJobAt(java.lang.String, java.lang.Object,
java.util.Map, java.util.Date)
      */
-    public void fireJobAt(String name, Object job, Map<String, Serializable> config,
Date date)
+    public void fireJobAt(final String name, final Object job, final Map<String, Serializable>
config, final Date date)
     throws SchedulerException {
-        if ( name == null ) {
-            name = PREFIX + UUID.randomUUID().toString();
+        final String jobName = this.getJobName(name);
+        final SimpleTrigger trigger = new SimpleTrigger(jobName, DEFAULT_QUARTZ_JOB_GROUP,
date);
+        this.scheduleJob(jobName, job, config, trigger, true);
+    }
+
+    /**
+     * @see org.apache.sling.commons.scheduler.Scheduler#fireJob(java.lang.Object, java.util.Map,
int, long)
+     */
+    public boolean fireJob(final Object job,
+                           final Map<String, Serializable> config,
+                           final int times,
+                           final long period) {
+        this.checkJob(job);
+        if ( times < 2 ) {
+            throw new IllegalArgumentException("Times argument must be higher than 1");
+        }
+        final long ms = period * 1000;
+        final String jobName = job.getClass().getName();
+        final JobDataMap dataMap = this.initDataMap(jobName, job, config, true);
+
+        final JobDetail detail = this.createJobDetail(jobName, dataMap);
+
+        final Trigger trigger = new SimpleTrigger(jobName, DEFAULT_QUARTZ_JOB_GROUP, times,
ms);
+        try {
+            this.scheduler.scheduleJob(detail, trigger);
+        } catch (final SchedulerException se) {
+            // ignore this and return false
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * @see org.apache.sling.commons.scheduler.Scheduler#fireJobAt(java.lang.String, java.lang.Object,
java.util.Map, java.util.Date, int, long)
+     */
+    public boolean fireJobAt(final String name,
+                             final Object job,
+                             final Map<String, Serializable> config,
+                             final Date date,
+                             final int times,
+                             final long period) {
+        if ( times < 2 ) {
+            throw new IllegalArgumentException("Times argument must be higher than 1");
+        }
+        final String jobName = job.getClass().getName();
+        final long ms = period * 1000;
+        final SimpleTrigger trigger = new SimpleTrigger(jobName, DEFAULT_QUARTZ_JOB_GROUP,
date, null, times, ms);
+        try {
+            this.scheduleJob(jobName, job, config, trigger, true);
+        } catch (final SchedulerException se) {
+            // ignore this and return false
+            return false;
         }
-        final SimpleTrigger trigger = new SimpleTrigger(name, DEFAULT_QUARTZ_JOB_GROUP, date);
-        this.scheduleJob(name, job, config, trigger, true);
+        return true;
+    }
+
+    /**
+     * Helper method which gets always a job name.
+     * @param name The job name or null
+     * @return The input job name or a unique job name.
+     */
+    private String getJobName(final String name) {
+        return name != null ? name : PREFIX + UUID.randomUUID();
     }
 
     /**
@@ -578,11 +638,4 @@
             this.executor = null;
         }
     }
-
-    protected static final class ConcurrentHandler {
-
-        public boolean runConcurrently;
-
-        public boolean isRunning = false;
-    }
 }



Mime
View raw message