Author: cziegeler
Date: Fri Jul 15 08:27:06 2016
New Revision: 1752797
URL: http://svn.apache.org/viewvc?rev=1752797&view=rev
Log:
SLING-5831 : Support different thread pools for scheduled tasks
Modified:
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
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/InternalScheduleOptions.java
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/SchedulerServiceFactory.java
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/package-info.java
sling/trunk/bundles/commons/scheduler/src/test/java/org/apache/sling/commons/scheduler/impl/QuartzSchedulerTest.java
Modified:
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java?rev=1752797&r1=1752796&r2=1752797&view=diff
==============================================================================
---
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
(original)
+++
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/ScheduleOptions.java
Fri Jul 15 08:27:06 2016
@@ -95,4 +95,19 @@ public interface ScheduleOptions {
* @return The schedule options.
*/
ScheduleOptions onInstancesOnly(String[] slingIds);
+
+ /**
+ * Define the thread pool to be used.
+ * Scheduled jobs can run using different thread pools. By default, the
default
+ * thread pool from the thread pool manager service is used.
+ * If a thread pool name is specified, a pool with that name will be get
from
+ * the thread pool manager. If such a pool does not exist, it will be
created.
+ * This option must be used with special care as it might create new
thread pools.
+ * It should only be used if there is a good reason to not use the default
thread
+ * pool.
+ * @param name The thread pool name
+ * @return The schedule options.
+ * @since 2.5.0
+ */
+ ScheduleOptions threadPoolName(String name);
}
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=1752797&r1=1752796&r2=1752797&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
Fri Jul 15 08:27:06 2016
@@ -102,6 +102,21 @@ public interface Scheduler {
String VALUE_RUN_ON_SINGLE = "SINGLE";
/**
+ * Name of the configuration property to define the thread pool to be used.
+ * Scheduled jobs can run using different thread pools. By default, the
default
+ * thread pool from the thread pool manager service is used.
+ * If a thread pool name is specified, a pool with that name will be get
from
+ * the thread pool manager. If such a pool does not exist, it will be
created.
+ * This option must be used with special care as it might create new
thread pools.
+ * It should only be used if there is a good reason to not use the default
thread
+ * pool.
+ * @param name The thread pool name
+ * @return The schedule options.
+ * @since 2.5.0
+ */
+ String PROPERTY_SCHEDULER_THREAD_POOL = "scheduler.threadPool";
+
+ /**
* Schedule a job based on the options.
*
* Note that if a job with the same name has already been added, the old
job is
@@ -145,7 +160,7 @@ public interface Scheduler {
/**
* Create a schedule options to fire a job immediately and only once.
- * @return The schedule options.
+ * @return The schedule options.
* @since 2.3
*/
ScheduleOptions NOW();
@@ -155,7 +170,7 @@ public interface Scheduler {
* @param times The number of times this job should be started (must be
higher than 1 or
* -1 for endless)
* @param period Every period seconds this job is started (must be at
higher than 0).
- * @return The schedule options.
+ * @return The schedule options.
* @since 2.3
*/
ScheduleOptions NOW(int times, long period);
@@ -163,7 +178,7 @@ public interface Scheduler {
/**
* Create a schedule options to fire a job once at a specific date
* @param date The date this job should be run.
- * @return The schedule options.
+ * @return The schedule options.
* @since 2.3
*/
ScheduleOptions AT(Date date);
@@ -174,7 +189,7 @@ public interface Scheduler {
* @param times The number of times this job should be started (must be
higher than 1 or
* -1 for endless)
* @param period Every period seconds this job is started (must be at
higher than 0).
- * @return The schedule options.
+ * @return The schedule options.
* @since 2.3
*/
ScheduleOptions AT(Date date, int times, long period);
@@ -182,7 +197,7 @@ public interface Scheduler {
/**
* Create a schedule options to schedule the job based on the expression
* @param expression The cron exception
- * @return The schedule options.
+ * @return The schedule options.
* @since 2.3
*/
ScheduleOptions EXPR(String expression);
Modified:
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java?rev=1752797&r1=1752796&r2=1752797&view=diff
==============================================================================
---
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java
(original)
+++
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/InternalScheduleOptions.java
Fri Jul 15 08:27:06 2016
@@ -32,6 +32,8 @@ public class InternalScheduleOptions imp
public String name;
+ public String threadPoolName;
+
public boolean canRunConcurrently = false;
public Map<String, Serializable> configuration;
@@ -55,6 +57,7 @@ public class InternalScheduleOptions imp
/**
* @see
org.apache.sling.commons.scheduler.ScheduleOptions#config(java.util.Map)
*/
+ @Override
public ScheduleOptions config(final Map<String, Serializable> config) {
this.configuration = config;
return this;
@@ -63,6 +66,7 @@ public class InternalScheduleOptions imp
/**
* @see
org.apache.sling.commons.scheduler.ScheduleOptions#name(java.lang.String)
*/
+ @Override
public ScheduleOptions name(final String name) {
this.name = name;
return this;
@@ -71,6 +75,7 @@ public class InternalScheduleOptions imp
/**
* @see
org.apache.sling.commons.scheduler.ScheduleOptions#canRunConcurrently(boolean)
*/
+ @Override
public ScheduleOptions canRunConcurrently(final boolean flag) {
this.canRunConcurrently = flag;
return this;
@@ -79,6 +84,7 @@ public class InternalScheduleOptions imp
/**
* @see
org.apache.sling.commons.scheduler.ScheduleOptions#onLeaderOnly(boolean)
*/
+ @Override
public ScheduleOptions onLeaderOnly(final boolean flag) {
if ( flag ) {
this.runOn = new String[] {Scheduler.VALUE_RUN_ON_LEADER};
@@ -91,6 +97,7 @@ public class InternalScheduleOptions imp
/**
* @see
org.apache.sling.commons.scheduler.ScheduleOptions#onSingleInstanceOnly(boolean)
*/
+ @Override
public ScheduleOptions onSingleInstanceOnly(final boolean flag) {
if ( flag ) {
this.runOn = new String[] {Scheduler.VALUE_RUN_ON_SINGLE};
@@ -103,8 +110,18 @@ public class InternalScheduleOptions imp
/**
* @see
org.apache.sling.commons.scheduler.ScheduleOptions#onInstancesOnly(java.lang.String[])
*/
+ @Override
public ScheduleOptions onInstancesOnly(final String[] slingIds) {
this.runOn = slingIds;
return this;
}
+
+ /**
+ * @see
org.apache.sling.commons.scheduler.ScheduleOptions#threadPoolName(java.lang.String)
+ */
+ @Override
+ public ScheduleOptions threadPoolName(final String name) {
+ this.threadPoolName = name;
+ return this;
+ }
}
Modified:
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/SchedulerServiceFactory.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/SchedulerServiceFactory.java?rev=1752797&r1=1752796&r2=1752797&view=diff
==============================================================================
---
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/SchedulerServiceFactory.java
(original)
+++
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/SchedulerServiceFactory.java
Fri Jul 15 08:27:06 2016
@@ -52,6 +52,7 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#schedule(java.lang.Object,
org.apache.sling.commons.scheduler.ScheduleOptions)
*/
+ @Override
public boolean schedule(final Object job, final ScheduleOptions options) {
return this.scheduler.schedule(this.bundleId, null, job, options);
}
@@ -59,6 +60,7 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#unschedule(java.lang.String)
*/
+ @Override
public boolean unschedule(final String jobName) {
return this.scheduler.unschedule(this.bundleId, jobName);
}
@@ -66,6 +68,7 @@ public class SchedulerServiceFactory imp
/**
* @see org.apache.sling.commons.scheduler.Scheduler#NOW()
*/
+ @Override
public ScheduleOptions NOW() {
return this.scheduler.NOW();
}
@@ -73,6 +76,7 @@ public class SchedulerServiceFactory imp
/**
* @see org.apache.sling.commons.scheduler.Scheduler#NOW(int, long)
*/
+ @Override
public ScheduleOptions NOW(final int times, final long period) {
return this.scheduler.NOW(times, period);
}
@@ -80,6 +84,7 @@ public class SchedulerServiceFactory imp
/**
* @see org.apache.sling.commons.scheduler.Scheduler#AT(java.util.Date)
*/
+ @Override
public ScheduleOptions AT(final Date date) {
return this.scheduler.AT(date);
}
@@ -87,6 +92,7 @@ public class SchedulerServiceFactory imp
/**
* @see org.apache.sling.commons.scheduler.Scheduler#AT(java.util.Date,
int, long)
*/
+ @Override
public ScheduleOptions AT(final Date date, final int times, final long
period) {
return this.scheduler.AT(date, times, period);
}
@@ -94,6 +100,7 @@ public class SchedulerServiceFactory imp
/**
* @see org.apache.sling.commons.scheduler.Scheduler#EXPR(java.lang.String)
*/
+ @Override
public ScheduleOptions EXPR(final String expression) {
return this.scheduler.EXPR(expression);
}
@@ -101,6 +108,8 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#addJob(java.lang.String,
java.lang.Object, java.util.Map, java.lang.String, boolean)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public void addJob(final String name, final Object job,
final Map<String, Serializable> config, final String
schedulingExpression,
final boolean canRunConcurrently) throws Exception {
@@ -110,6 +119,8 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#addPeriodicJob(java.lang.String,
java.lang.Object, java.util.Map, long, boolean)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public void addPeriodicJob(final String name, final Object job,
final Map<String, Serializable> config, final long period,
final boolean canRunConcurrently) throws Exception {
@@ -119,6 +130,8 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#addPeriodicJob(java.lang.String,
java.lang.Object, java.util.Map, long, boolean, boolean)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public void addPeriodicJob(final String name, final Object job,
final Map<String, Serializable> config, final long period,
final boolean canRunConcurrently, final boolean startImmediate)
@@ -129,6 +142,8 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#fireJob(java.lang.Object,
java.util.Map)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public void fireJob(final Object job, final Map<String, Serializable>
config)
throws Exception {
this.scheduler.fireJob(this.bundleId, null, job, config);
@@ -137,6 +152,8 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#fireJob(java.lang.Object,
java.util.Map, int, long)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public boolean fireJob(final Object job, final Map<String, Serializable>
config,
final int times, final long period) {
return this.scheduler.fireJob(this.bundleId, null, job, config, times,
period);
@@ -145,6 +162,8 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#fireJobAt(java.lang.String,
java.lang.Object, java.util.Map, java.util.Date)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public void fireJobAt(final String name, final Object job,
final Map<String, Serializable> config, final Date date) throws
Exception {
this.scheduler.fireJobAt(this.bundleId, null, name, job, config, date);
@@ -153,11 +172,18 @@ public class SchedulerServiceFactory imp
/**
* @see
org.apache.sling.commons.scheduler.Scheduler#fireJobAt(java.lang.String,
java.lang.Object, java.util.Map, java.util.Date, int, long)
*/
+ @Override
+ @SuppressWarnings("deprecation")
public boolean fireJobAt(final String name, final Object job,
final Map<String, Serializable> config, final Date date, final int
times, final long period) {
return this.scheduler.fireJobAt(this.bundleId, null, name, job,
config, date, times, period);
}
+ /**
+ * @see
org.apache.sling.commons.scheduler.Scheduler#removeJob(java.lang.String)
+ */
+ @Override
+ @SuppressWarnings("deprecation")
public void removeJob(final String name) throws NoSuchElementException {
this.scheduler.removeJob(this.bundleId, name);
}
Modified:
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java?rev=1752797&r1=1752796&r2=1752797&view=diff
==============================================================================
---
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
(original)
+++
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/impl/WhiteboardHandler.java
Fri Jul 15 08:27:06 2016
@@ -67,16 +67,19 @@ public class WhiteboardHandler {
btx.createFilter(SCHEDULED_JOB_FILTER),
new ServiceTrackerCustomizer() {
+ @Override
public void removedService(final ServiceReference reference,
final Object service) {
unregister(reference);
btx.ungetService(reference);
}
+ @Override
public void modifiedService(final ServiceReference reference,
final Object service) {
unregister(reference);
register(reference, service);
}
+ @Override
public Object addingService(final ServiceReference reference) {
final Object obj = btx.getService(reference);
if ( obj != null ) {
@@ -252,9 +255,18 @@ public class WhiteboardHandler {
final Boolean concurrent = getBooleanProperty(ref,
Scheduler.PROPERTY_SCHEDULER_CONCURRENT);
final String[] runOnOpts = getRunOpts(ref);
+ final Object poolNameObj =
ref.getProperty(Scheduler.PROPERTY_SCHEDULER_THREAD_POOL);
+ final String poolName;
+ if ( poolNameObj != null && poolNameObj.toString().trim().length() > 0
) {
+ poolName = poolNameObj.toString().trim();
+ } else {
+ poolName = null;
+ }
+
final ScheduleOptions options = scheduleOptions
.name(name)
.canRunConcurrently((concurrent != null ? concurrent : true))
+ .threadPoolName(poolName)
.onInstancesOnly(runOnOpts);
final long bundleId = ref.getBundle().getBundleId();
Modified:
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/package-info.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/package-info.java?rev=1752797&r1=1752796&r2=1752797&view=diff
==============================================================================
---
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/package-info.java
(original)
+++
sling/trunk/bundles/commons/scheduler/src/main/java/org/apache/sling/commons/scheduler/package-info.java
Fri Jul 15 08:27:06 2016
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("2.4.0")
+@Version("2.5.0")
package org.apache.sling.commons.scheduler;
import aQute.bnd.annotation.Version;
Modified:
sling/trunk/bundles/commons/scheduler/src/test/java/org/apache/sling/commons/scheduler/impl/QuartzSchedulerTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/scheduler/src/test/java/org/apache/sling/commons/scheduler/impl/QuartzSchedulerTest.java?rev=1752797&r1=1752796&r2=1752797&view=diff
==============================================================================
---
sling/trunk/bundles/commons/scheduler/src/test/java/org/apache/sling/commons/scheduler/impl/QuartzSchedulerTest.java
(original)
+++
sling/trunk/bundles/commons/scheduler/src/test/java/org/apache/sling/commons/scheduler/impl/QuartzSchedulerTest.java
Fri Jul 15 08:27:06 2016
@@ -16,18 +16,6 @@
*/
package org.apache.sling.commons.scheduler.impl;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.when;
-
-import java.io.Serializable;
-import java.lang.reflect.Field;
-import java.util.Date;
-import java.util.HashMap;
-
import org.apache.sling.commons.scheduler.Job;
import org.apache.sling.testing.mock.osgi.MockOsgi;
import org.junit.After;
@@ -46,9 +34,17 @@ import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.util.Date;
+import java.util.HashMap;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.when;
+
@RunWith(MockitoJUnitRunner.class)
public class QuartzSchedulerTest {
-
+
private Scheduler s;
private SchedulerProxy proxy;
private BundleContext context;