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 2007/09/25 17:21:16 UTC
svn commit: r579296 - in /incubator/sling/trunk/scheduler: ./
src/main/java/org/apache/sling/scheduler/
src/main/java/org/apache/sling/scheduler/impl/
Author: cziegeler
Date: Tue Sep 25 08:21:14 2007
New Revision: 579296
URL: http://svn.apache.org/viewvc?rev=579296&view=rev
Log:
Add a new job interface to make configurable jobs possible.
Start adding support for scheduled events.
Added:
incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java (with props)
incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java (with props)
Modified:
incubator/sling/trunk/scheduler/pom.xml
incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java
incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java
incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java
Modified: incubator/sling/trunk/scheduler/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/pom.xml?rev=579296&r1=579295&r2=579296&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/pom.xml (original)
+++ incubator/sling/trunk/scheduler/pom.xml Tue Sep 25 08:21:14 2007
@@ -101,6 +101,11 @@
<artifactId>quartz</artifactId>
<version>1.5.2</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling-core</artifactId>
+ <version>2.0.0-incubator-SNAPSHOT</version>
+ </dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
Added: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java?rev=579296&view=auto
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java (added)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java Tue Sep 25 08:21:14 2007
@@ -0,0 +1,42 @@
+/*
+ * 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.scheduler;
+
+import java.util.Dictionary;
+
+/**
+ * This is an utility implementation for sending timed events.
+ */
+public class EventJob implements Runnable {
+
+ protected final String eventTopic;
+
+ protected final Dictionary eventProperties;
+
+ public EventJob(String topic, Dictionary properties) {
+ this.eventTopic = topic;
+ this.eventProperties = properties;
+ }
+
+ /**
+ * @see java.lang.Runnable#run()
+ */
+ public void run() {
+ // TODO Auto-generated method stub
+
+ }
+}
Propchange: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java
------------------------------------------------------------------------------
svn:keywords = author date id revision url
Added: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java?rev=579296&view=auto
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java (added)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java Tue Sep 25 08:21:14 2007
@@ -0,0 +1,28 @@
+/*
+ * 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.scheduler;
+
+import java.util.Map;
+
+import org.apache.sling.core.ServiceLocator;
+
+public interface Job {
+
+ void execute(String name,
+ Map configuration,
+ ServiceLocator locator);
+}
Propchange: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java
------------------------------------------------------------------------------
svn:keywords = author date id revision url
Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java?rev=579296&r1=579295&r2=579296&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java Tue Sep 25 08:21:14 2007
@@ -22,53 +22,59 @@
/**
* A scheduler to schedule time/cron based jobs.
- * The fired jobs can either implement the Runnable interface or the Quartz job interface.
+ * A job is an object which is executed/fired by the scheduler. The object
+ * should either implement the {@link Job} interface or the {@link Runnable}
+ * interface.
*/
public interface Scheduler {
/**
- * Schedule a time based job. Note that if a job with the same name has already beed added it is overwritten.
- *
- * @param name the name of the job
- * @param job The job object itself. It must implement either Runnable or might also be an implementation
- * specific class (i.e. org.quartz.Job)
- * @param schedulingExpression the time specification using a scheduling expression
- * @param canRunConcurrently whether this job can run even previous scheduled runs are still running
+ * Schedule a time based job.
+ * 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 schedulingExpression The time specification using a scheduling expression.
+ * @param canRunConcurrently Whether this job can run even if previous scheduled runs are still running.
*/
void addJob(String name, Object job, Map<Object, Object> config, String schedulingExpression, boolean canRunConcurrently)
throws Exception;
/**
- * Schedule a periodic job. The job is started the first time when the period has passed. Note that if a job with
- * the same name has already beed added it is overwritten.
- *
- * @param name the name of the job
- * @param job The job object itself. It must implement either Runnable or might also be an implementation
- * specific class (i.e. org.quartz.Job)
- * @param period Every period seconds this job is started
- * @param canRunConcurrently whether this job can run even previous scheduled runs are still running
+ * Schedule a periodic job.
+ * The job is started the first time when the period has passed.
+ * 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 period Every period seconds this job is started.
+ * @param canRunConcurrently Whether this job can run even if previous scheduled runs are still running.
*/
void addPeriodicJob(String name, Object job, Map<Object, Object> config, long period, boolean canRunConcurrently)
throws Exception;
/**
- * Fire a job once immediately
- *
- * @param job The job object itself. It must implement either Runnable or might also be an implementation
- * specific class (i.e. org.quartz.Job)
+ * Fire a job immediately and only 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}.
*/
void fireJob(Object job, Map<Object, Object> config)
throws Exception;
/**
- * Fire a job once at a specific date Note that if a job with the same name has already beed added it is
- * overwritten.
- *
- * @param date The date this job should be scheduled
- * @param name the name of the job
- * @param job The job object itself. It must implement either Runnable or might also be an implementation
- * specific class (i.e. org.quartz.Job)
+ * 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.
+ *
+ * @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.
*/
void fireJobAt(String name, Object job, Map<Object, Object> config, Date date)
throws Exception;
@@ -76,7 +82,7 @@
/**
* Remove a scheduled job by name.
*
- * @param name the name of the job
+ * @param name The name of the job.
*/
void removeJob(String name)
throws NoSuchElementException;
Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java?rev=579296&r1=579295&r2=579296&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java Tue Sep 25 08:21:14 2007
@@ -16,13 +16,16 @@
*/
package org.apache.sling.scheduler.impl;
+import java.util.Map;
+
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
- * This component is resposible to launch a {@link Job} or {@link Runnable} in a Quartz Scheduler.
+ * This component is resposible to launch a {@link org.apache.sling.scheduler.Job}
+ * or {@link Runnable} in a Quartz Scheduler.
*
*/
public class QuartzJobExecutor implements Job {
@@ -48,9 +51,10 @@
try {
final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
-
- if (job instanceof Job) {
- ((Job) job).execute(context);
+ final Map<Object, Object> configuration = (Map<Object, Object>) data.get(QuartzScheduler.DATA_MAP_CONFIGURATION);
+ final String name = (String) data.get(QuartzScheduler.DATA_MAP_NAME);
+ if (job instanceof org.apache.sling.scheduler.Job) {
+ ((org.apache.sling.scheduler.Job) job).execute(name, configuration, null);
} else if (job instanceof Runnable) {
((Runnable) job).run();
}
Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java?rev=579296&r1=579295&r2=579296&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java Tue Sep 25 08:21:14 2007
@@ -22,13 +22,14 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
+import java.util.UUID;
+import org.apache.sling.scheduler.Job;
import org.apache.sling.scheduler.Scheduler;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.quartz.CronTrigger;
-import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
@@ -44,7 +45,7 @@
*
* @scr.component
* @scr.service interface="org.apache.sling.scheduler.Scheduler"
- * @scr.reference name="job" interface="org.quartz.Job" cardinality="0..n" policy="dynamic" bind="bindJob" unbind="unbindJob"
+ * @scr.reference name="job" interface="org.apache.sling.scheduler.Job" cardinality="0..n" policy="dynamic" bind="bindJob" unbind="unbindJob"
* @scr.reference name="task" interface="java.lang.Runnable" cardinality="0..n" policy="dynamic" bind="bindJob" unbind="unbindJob"
*/
public class QuartzScheduler implements Scheduler {
@@ -66,12 +67,21 @@
/** Map key for the run status */
static final String DATA_MAP_KEY_ISRUNNING = "QuartzJobExecutor.isRunning";
+ /** Map key for the configuration. */
+ static final String DATA_MAP_CONFIGURATION = "QuartzJobScheduler.Configuration";
+
protected org.quartz.Scheduler scheduler;
protected final List<ServiceReference> registeredJobs = new ArrayList<ServiceReference>();
protected ComponentContext context;
+ /**
+ * Activate this component.
+ * Start the scheduler.
+ * @param ctx The component context.
+ * @throws Exception
+ */
protected void activate(ComponentContext ctx) throws Exception {
this.context = ctx;
synchronized ( this.registeredJobs ) {
@@ -89,6 +99,11 @@
}
}
+ /**
+ * Deactivate this component.
+ * Stop the scheduler.
+ * @param ctx The component context.
+ */
protected void deactivate(ComponentContext ctx) {
synchronized (this.registeredJobs ) {
this.dispose();
@@ -122,14 +137,14 @@
/**
* Add a job to the scheduler
*
- * @param name The name of the job to add
+ * @param name The name of the job to add (or null)
* @param Tje jopb
* @param trigger a Trigger
* @param canRunConcurrently whether this job can be run concurrently
*
* @throws Exception thrown in case of errors
*/
- protected void scheduleJob(final String name,
+ protected void scheduleJob(String name,
final Object job,
final Map<Object, Object> config,
final Trigger trigger,
@@ -139,12 +154,16 @@
this.checkJob(job);
// if there is already a job with the name, remove it first
- try {
- final JobDetail jobdetail = this.scheduler.getJobDetail(name, DEFAULT_QUARTZ_JOB_GROUP);
- if (jobdetail != null) {
- this.removeJob(name);
+ if ( name != null ) {
+ try {
+ final JobDetail jobdetail = this.scheduler.getJobDetail(name, DEFAULT_QUARTZ_JOB_GROUP);
+ if (jobdetail != null) {
+ this.removeJob(name);
+ }
+ } catch (final SchedulerException ignored) {
}
- } catch (final SchedulerException ignored) {
+ } else {
+ name = "Sling Quartz Scheduler " + UUID.randomUUID().toString();
}
// create the data map
@@ -155,23 +174,37 @@
this.scheduler.scheduleJob(detail, trigger);
}
+ /**
+ * Initialize the data map for the job executor.
+ * @param jobName
+ * @param job
+ * @param config
+ * @param concurent
+ * @return
+ */
protected JobDataMap initDataMap(String jobName,
Object job,
- Map<Object, Object> config,
+ Map<Object, Object> config,
boolean concurent) {
final JobDataMap jobDataMap = new JobDataMap();
- // if config is supplied copy all entries first
- if ( config != null ) {
- jobDataMap.putAll(config);
- }
+
jobDataMap.put(DATA_MAP_OBJECT, job);
jobDataMap.put(DATA_MAP_NAME, jobName);
jobDataMap.put(DATA_MAP_RUN_CONCURRENT, (concurent? Boolean.TRUE: Boolean.FALSE));
+ if ( config != null ) {
+ jobDataMap.put(DATA_MAP_CONFIGURATION, config);
+ }
return jobDataMap;
}
+ /**
+ * Create the job detail.
+ * @param name
+ * @param jobDataMap
+ * @return
+ */
protected JobDetail createJobDetail(String name, JobDataMap jobDataMap) {
final JobDetail detail = new JobDetail(name, DEFAULT_QUARTZ_JOB_GROUP, QuartzJobExecutor.class);
detail.setJobDataMap(jobDataMap);
@@ -213,6 +246,9 @@
public void addPeriodicJob(String name, Object job, Map<Object, Object> config, long period, boolean canRunConcurrently)
throws Exception {
final long ms = period * 1000;
+ if ( name == null ) {
+ name = "Sling Quartz Scheduler " + UUID.randomUUID().toString();
+ }
final SimpleTrigger timeEntry =
new SimpleTrigger(name, DEFAULT_QUARTZ_JOB_GROUP, new Date(System.currentTimeMillis() + ms), null,
SimpleTrigger.REPEAT_INDEFINITELY, ms);
@@ -239,6 +275,9 @@
* @see org.apache.sling.core.scheduler.Scheduler#fireJobAt(java.lang.String, java.lang.Object, java.util.Map, java.util.Date)
*/
public void fireJobAt(String name, Object job, Map<Object, Object> config, Date date) throws Exception {
+ if ( name == null ) {
+ name = "Sling Quartz Scheduler " + UUID.randomUUID().toString();
+ }
final SimpleTrigger trigger = new SimpleTrigger(name, DEFAULT_QUARTZ_JOB_GROUP, date);
this.scheduleJob(name, job, config, trigger, true);
}
@@ -257,7 +296,7 @@
protected void register(ServiceReference ref)
throws Exception {
// get the job
- final Object job = this.context.getBundleContext().getService(ref);
+ final Object job = this.context.locateService("job", ref);
if ( ref != null ) {
this.checkJob(job);
String name = (String)ref.getProperty("scheduler.name");
@@ -291,6 +330,11 @@
}
}
+ /**
+ * Bind a new job.
+ * @param ref
+ * @throws Exception
+ */
protected void bindJob(ServiceReference ref)
throws Exception {
synchronized ( this.registeredJobs ) {
@@ -302,6 +346,10 @@
}
}
+ /**
+ * Unbind a job.
+ * @param ref
+ */
protected void unbindJob(ServiceReference ref) {
synchronized ( this.registeredJobs ) {
if ( this.scheduler != null ) {