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 2013/10/04 18:11:10 UTC

svn commit: r1529204 - in /sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event: impl/dea/ impl/jobs/ impl/jobs/timed/ jobs/

Author: cziegeler
Date: Fri Oct  4 16:11:10 2013
New Revision: 1529204

URL: http://svn.apache.org/r1529204
Log:
SLING-3139 : Provide a way to schedule jobs
SLING-3138 : Add fluent api to create new jobs

Added:
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java   (with props)
Modified:
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/dea/DistributedEventReceiver.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobBuilderImpl.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventReceiver.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventSender.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobBuilder.java
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobManager.java

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/dea/DistributedEventReceiver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/dea/DistributedEventReceiver.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/dea/DistributedEventReceiver.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/dea/DistributedEventReceiver.java Fri Oct  4 16:11:10 2013
@@ -45,6 +45,7 @@ import org.apache.sling.discovery.Topolo
 import org.apache.sling.discovery.TopologyEvent.Type;
 import org.apache.sling.discovery.TopologyEventListener;
 import org.apache.sling.event.EventUtil;
+import org.apache.sling.event.impl.jobs.Utility;
 import org.apache.sling.event.impl.support.BatchResourceRemover;
 import org.apache.sling.event.impl.support.Environment;
 import org.apache.sling.event.impl.support.ResourceHelper;
@@ -135,7 +136,7 @@ public class DistributedEventReceiver
         // stop background threads by putting empty objects into the queue
         this.running = false;
         try {
-            this.writeQueue.put(new Event("some", (Dictionary<String, Object>)null));
+            this.writeQueue.put(new Event(Utility.TOPIC_STOPPED, (Dictionary<String, Object>)null));
         } catch (final InterruptedException e) {
             this.ignoreException(e);
         }

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobBuilderImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobBuilderImpl.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobBuilderImpl.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobBuilderImpl.java Fri Oct  4 16:11:10 2013
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.event.impl.jobs;
 
+import java.util.Date;
 import java.util.Map;
 
 import org.apache.sling.event.jobs.Job;
@@ -63,4 +64,41 @@ public class JobBuilderImpl implements J
     public ScheduleBuilder schedule(final String name) {
         return null;
     }
+
+    public final class ScheduleBuilderImpl implements ScheduleBuilder {
+
+        @Override
+        public boolean periodically(int minutes) {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        @Override
+        public TimeBuilder daily() {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public TimeBuilder weekly(int day) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public boolean at(Date date) {
+            // TODO Auto-generated method stub
+            return false;
+        }
+
+        public final class TimeBuilderImpl implements TimeBuilder {
+
+            @Override
+            public boolean at(int hour, int minute) {
+                // TODO Auto-generated method stub
+                return false;
+            }
+
+        }
+    }
 }

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java Fri Oct  4 16:11:10 2013
@@ -78,6 +78,7 @@ import org.apache.sling.event.jobs.JobUt
 import org.apache.sling.event.jobs.JobsIterator;
 import org.apache.sling.event.jobs.Queue;
 import org.apache.sling.event.jobs.QueueConfiguration;
+import org.apache.sling.event.jobs.ScheduledJobInfo;
 import org.apache.sling.event.jobs.Statistics;
 import org.apache.sling.event.jobs.TopicStatistics;
 import org.apache.sling.event.jobs.consumer.JobExecutor;
@@ -1416,6 +1417,8 @@ public class JobManagerImpl
      */
     @Override
     public void stopJobById(final String jobId) {
+        // 1. check if the job is running locally - stop directly
+        // 2. if running remote, send an event via event admin to stop
         // TODO not implemented yet
         throw new IllegalStateException("Not implemented yet...");
     }
@@ -1427,4 +1430,16 @@ public class JobManagerImpl
     public JobBuilder createJob(final String topic) {
         return new JobBuilderImpl(this, topic);
     }
+
+    @Override
+    public Collection<ScheduledJobInfo> getScheduledJobs() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public ScheduledJobInfo getScheduledJob(String name) {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java Fri Oct  4 16:11:10 2013
@@ -37,6 +37,8 @@ public abstract class Utility {
     public static final String PROPERTY_LOCK_CREATED_APP = "lock.created.app";
     public static final String RESOURCE_TYPE_LOCK = "slingevent:Lock";
 
+    public static final String TOPIC_STOPPED = "org/apache/sling/event/impl/jobs/STOP";
+
     /**
      * Check the job topic.
      * @return <code>null</code> if the topic is correct, otherwise an error description is returned

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventReceiver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventReceiver.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventReceiver.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventReceiver.java Fri Oct  4 16:11:10 2013
@@ -37,6 +37,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.event.EventUtil;
+import org.apache.sling.event.impl.jobs.Utility;
 import org.apache.sling.event.impl.support.ResourceHelper;
 import org.apache.sling.event.jobs.JobUtil;
 import org.osgi.service.event.Event;
@@ -121,7 +122,7 @@ public class TimedEventReceiver implemen
         // stop background threads by putting empty objects into the queue
         this.running = false;
         try {
-            this.writeQueue.put(new Event("some", (Dictionary<String, Object>)null));
+            this.writeQueue.put(new Event(Utility.TOPIC_STOPPED, (Dictionary<String, Object>)null));
         } catch (final InterruptedException e) {
             this.ignoreException(e);
         }

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventSender.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventSender.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventSender.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/timed/TimedEventSender.java Fri Oct  4 16:11:10 2013
@@ -59,6 +59,7 @@ import org.apache.sling.discovery.Topolo
 import org.apache.sling.discovery.TopologyEventListener;
 import org.apache.sling.event.EventUtil;
 import org.apache.sling.event.TimedEventStatusProvider;
+import org.apache.sling.event.impl.jobs.Utility;
 import org.apache.sling.event.impl.support.Environment;
 import org.apache.sling.event.impl.support.ResourceHelper;
 import org.apache.sling.event.jobs.JobUtil;
@@ -154,7 +155,7 @@ public class TimedEventSender
         // stop background threads by putting empty objects into the queue
         this.queue.clear();
         try {
-            this.queue.put(new Event("some", (Dictionary<String, Object>)null));
+            this.queue.put(new Event(Utility.TOPIC_STOPPED, (Dictionary<String, Object>)null));
         } catch (final InterruptedException e) {
             this.ignoreException(e);
         }
@@ -234,7 +235,7 @@ public class TimedEventSender
                     }
                     event = null;
 
-                } else {
+                } else if ( !Utility.TOPIC_STOPPED.equals(event.getTopic()) ) {
                     ScheduleInfo scheduleInfo = null;
                     try {
                         scheduleInfo = new ScheduleInfo(event);

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobBuilder.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobBuilder.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobBuilder.java Fri Oct  4 16:11:10 2013
@@ -18,30 +18,85 @@
  */
 package org.apache.sling.event.jobs;
 
+import java.util.Date;
 import java.util.Map;
 
 import aQute.bnd.annotation.ProviderType;
 
 /**
+ * This is a builder interface to build jobs and scheduled jobs.
+ * Instances of this class can be retrieved using {@link JobManager#createJob(String)}
+ *
  * @since 1.3
  */
 @ProviderType
 public interface JobBuilder {
 
+    /**
+     * Set the optional job name
+     */
     JobBuilder name(final String name);
 
+    /**
+     * Set the optional configuration properties for the job.
+     */
     JobBuilder properties(final Map<String, Object> props);
 
+    /**
+     * Add the job.
+     * @see JobManager#addJob(String, Map)
+     * @return The job or <code>null</code>
+     */
     Job add();
 
+    /**
+     * Schedule the job
+     * If a job scheduler with the same name already exists, it is updated
+     * with the new information.
+     * @param name Unique name for the scheduler.
+     * @return A schedule builder to schedule the jobs
+     */
     ScheduleBuilder schedule(final String name);
 
+    /**
+     * This is a builder interface for creating schedule information
+     */
     public interface ScheduleBuilder {
 
+        /**
+         * Schedule the job periodically every N minutes
+         * @param minutes Positive number of minutes
+         * @return <code>true</code> if the job could be scheduled, <code>false</code>otherwise.
+         */
         boolean periodically(final int minutes);
 
-        boolean daily(final int hour, final int minute);
+        /**
+         * Schedule the job daily, the time needs to be specified in addition.
+         */
+        TimeBuilder daily();
+
+        /**
+         * Schedule the job weekly, the time needs to be specified in addition.
+         * @param day Day of the week, Sunday being one, Monday two, up to Saturday being seven.
+         */
+        TimeBuilder weekly(final int day);
+
+        /**
+         * Schedule the job for a specific date.
+         * @param date The date
+         * @return <code>true</code> if the job could be scheduled, <code>false</code>otherwise.
+         */
+        boolean at(final Date date);
+    }
+
+    public interface TimeBuilder {
 
-        boolean weekly(final int day, final int hour, final int minute);
+        /**
+         * Schedule the job for the given hour and minute.
+         * @param hour  Hour of the day ranging from 0 to 23.
+         * @param minute Minute of the hour ranging from 0 to 59.
+         * @return <code>true</code> if the job could be scheduled, <code>false</code>otherwise.
+         */
+        boolean at(final int hour, final int minute);
     }
 }
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobManager.java?rev=1529204&r1=1529203&r2=1529204&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobManager.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/JobManager.java Fri Oct  4 16:11:10 2013
@@ -215,6 +215,16 @@ public interface JobManager {
     JobBuilder createJob(final String topic);
 
     /**
+     * @since 1.3
+     */
+    Collection<ScheduledJobInfo> getScheduledJobs();
+
+    /**
+     * @since 1.3
+     */
+    ScheduledJobInfo getScheduledJob(final String name);
+
+    /**
      * Return all jobs either running or scheduled.
      *
      * @param type Required parameter for the type: either all jobs, only queued or only started can be returned.

Added: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java?rev=1529204&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java (added)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java Fri Oct  4 16:11:10 2013
@@ -0,0 +1,57 @@
+/*
+ * 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.event.jobs;
+
+import java.util.Date;
+import java.util.Map;
+
+import aQute.bnd.annotation.ProviderType;
+
+@ProviderType
+public interface ScheduledJobInfo {
+
+    enum ScheduleType {
+        DATE,
+        PERIODICALLY,
+        DAILY,
+        WEEKLY
+    }
+
+    String getName();
+
+    ScheduleType getScheduleType();
+
+    Date getNextScheduledExecution();
+
+    int getDayOfWeek();
+
+    int getHourOfDay();
+
+    int getMinuteOfHour();
+
+    int getPeriod();
+
+    String getJobTopic();
+
+    String getJobName();
+
+    Map<String, Object> getJobProperties();
+
+    void unschedule();
+}

Propchange: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/jobs/ScheduledJobInfo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain