11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2013 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2323import java .util .concurrent .ScheduledExecutorService ;
2424import java .util .concurrent .ScheduledFuture ;
2525import java .util .concurrent .TimeUnit ;
26+ import javax .enterprise .concurrent .LastExecution ;
27+ import javax .enterprise .concurrent .ManagedScheduledExecutorService ;
2628
2729import org .springframework .core .task .TaskRejectedException ;
2830import org .springframework .scheduling .TaskScheduler ;
2931import org .springframework .scheduling .Trigger ;
32+ import org .springframework .scheduling .support .SimpleTriggerContext ;
3033import org .springframework .scheduling .support .TaskUtils ;
3134import org .springframework .util .Assert ;
35+ import org .springframework .util .ClassUtils ;
3236import org .springframework .util .ErrorHandler ;
3337
3438/**
3741 * Extends {@link ConcurrentTaskExecutor} in order to implement the
3842 * {@link org.springframework.scheduling.SchedulingTaskExecutor} interface as well.
3943 *
44+ * <p>Autodetects a JSR-236 {@link javax.enterprise.concurrent.ManagedScheduledExecutorService}
45+ * in order to use it for trigger-based scheduling if possible, instead of Spring's
46+ * local trigger management which ends up delegating to regular delay-based scheduling
47+ * against the {@code java.util.concurrent.ScheduledExecutorService} API.
48+ *
4049 * <p>Note that there is a pre-built {@link ThreadPoolTaskScheduler} that allows for
4150 * defining a JDK 1.5 {@link java.util.concurrent.ScheduledThreadPoolExecutor} in bean style,
4251 * exposing it as a Spring {@link org.springframework.scheduling.TaskScheduler} directly.
5362 */
5463public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements TaskScheduler {
5564
56- private volatile ScheduledExecutorService scheduledExecutor ;
65+ private static Class <?> managedScheduledExecutorService ;
5766
58- private volatile ErrorHandler errorHandler ;
67+ static {
68+ try {
69+ managedScheduledExecutorService = ClassUtils .forName (
70+ "javax.enterprise.concurrent.ManagedScheduledExecutorService" ,
71+ ConcurrentTaskScheduler .class .getClassLoader ());
72+ }
73+ catch (ClassNotFoundException ex ) {
74+ // JSR-236 API not available...
75+ managedScheduledExecutorService = null ;
76+ }
77+ }
78+
79+ private ScheduledExecutorService scheduledExecutor ;
80+
81+ private boolean enterpriseConcurrentScheduler = false ;
82+
83+ private ErrorHandler errorHandler ;
5984
6085
6186 /**
@@ -71,6 +96,9 @@ public ConcurrentTaskScheduler() {
7196 /**
7297 * Create a new ConcurrentTaskScheduler,
7398 * using the given JDK 1.5 executor as shared delegate.
99+ * <p>Autodetects a JSR-236 {@link javax.enterprise.concurrent.ManagedScheduledExecutorService}
100+ * in order to use it for trigger-based scheduling if possible,
101+ * instead of Spring's local trigger management.
74102 * @param scheduledExecutor the JDK 1.5 scheduled executor to delegate to
75103 * for {@link org.springframework.scheduling.SchedulingTaskExecutor} as well
76104 * as {@link TaskScheduler} invocations
@@ -83,6 +111,9 @@ public ConcurrentTaskScheduler(ScheduledExecutorService scheduledExecutor) {
83111 /**
84112 * Create a new ConcurrentTaskScheduler,
85113 * using the given JDK 1.5 executors as delegates.
114+ * <p>Autodetects a JSR-236 {@link javax.enterprise.concurrent.ManagedScheduledExecutorService}
115+ * in order to use it for trigger-based scheduling if possible,
116+ * instead of Spring's local trigger management.
86117 * @param concurrentExecutor the JDK 1.5 concurrent executor to delegate to
87118 * for {@link org.springframework.scheduling.SchedulingTaskExecutor} invocations
88119 * @param scheduledExecutor the JDK 1.5 scheduled executor to delegate to
@@ -96,15 +127,25 @@ public ConcurrentTaskScheduler(Executor concurrentExecutor, ScheduledExecutorSer
96127
97128 /**
98129 * Specify the JDK 1.5 scheduled executor to delegate to.
130+ * <p>Autodetects a JSR-236 {@link javax.enterprise.concurrent.ManagedScheduledExecutorService}
131+ * in order to use it for trigger-based scheduling if possible,
132+ * instead of Spring's local trigger management.
99133 * <p>Note: This will only apply to {@link TaskScheduler} invocations.
100134 * If you want the given executor to apply to
101135 * {@link org.springframework.scheduling.SchedulingTaskExecutor} invocations
102136 * as well, pass the same executor reference to {@link #setConcurrentExecutor}.
103137 * @see #setConcurrentExecutor
104138 */
105139 public final void setScheduledExecutor (ScheduledExecutorService scheduledExecutor ) {
106- this .scheduledExecutor =
107- (scheduledExecutor != null ? scheduledExecutor : Executors .newSingleThreadScheduledExecutor ());
140+ if (scheduledExecutor != null ) {
141+ this .scheduledExecutor = scheduledExecutor ;
142+ this .enterpriseConcurrentScheduler = (managedScheduledExecutorService != null &&
143+ managedScheduledExecutorService .isInstance (scheduledExecutor ));
144+ }
145+ else {
146+ this .scheduledExecutor = Executors .newSingleThreadScheduledExecutor ();
147+ this .enterpriseConcurrentScheduler = false ;
148+ }
108149 }
109150
110151 /**
@@ -118,9 +159,13 @@ public void setErrorHandler(ErrorHandler errorHandler) {
118159
119160 public ScheduledFuture schedule (Runnable task , Trigger trigger ) {
120161 try {
121- ErrorHandler errorHandler =
122- (this .errorHandler != null ? this .errorHandler : TaskUtils .getDefaultErrorHandler (true ));
123- return new ReschedulingRunnable (task , trigger , this .scheduledExecutor , errorHandler ).schedule ();
162+ if (this .enterpriseConcurrentScheduler ) {
163+ return new EnterpriseConcurrentTriggerScheduler ().schedule (decorateTask (task , true ), trigger );
164+ }
165+ else {
166+ ErrorHandler errorHandler = (this .errorHandler != null ? this .errorHandler : TaskUtils .getDefaultErrorHandler (true ));
167+ return new ReschedulingRunnable (task , trigger , this .scheduledExecutor , errorHandler ).schedule ();
168+ }
124169 }
125170 catch (RejectedExecutionException ex ) {
126171 throw new TaskRejectedException ("Executor [" + this .scheduledExecutor + "] did not accept task: " + task , ex );
@@ -130,8 +175,7 @@ public ScheduledFuture schedule(Runnable task, Trigger trigger) {
130175 public ScheduledFuture schedule (Runnable task , Date startTime ) {
131176 long initialDelay = startTime .getTime () - System .currentTimeMillis ();
132177 try {
133- return this .scheduledExecutor .schedule (
134- errorHandlingTask (task , false ), initialDelay , TimeUnit .MILLISECONDS );
178+ return this .scheduledExecutor .schedule (decorateTask (task , false ), initialDelay , TimeUnit .MILLISECONDS );
135179 }
136180 catch (RejectedExecutionException ex ) {
137181 throw new TaskRejectedException ("Executor [" + this .scheduledExecutor + "] did not accept task: " + task , ex );
@@ -141,8 +185,7 @@ public ScheduledFuture schedule(Runnable task, Date startTime) {
141185 public ScheduledFuture scheduleAtFixedRate (Runnable task , Date startTime , long period ) {
142186 long initialDelay = startTime .getTime () - System .currentTimeMillis ();
143187 try {
144- return this .scheduledExecutor .scheduleAtFixedRate (
145- errorHandlingTask (task , true ), initialDelay , period , TimeUnit .MILLISECONDS );
188+ return this .scheduledExecutor .scheduleAtFixedRate (decorateTask (task , true ), initialDelay , period , TimeUnit .MILLISECONDS );
146189 }
147190 catch (RejectedExecutionException ex ) {
148191 throw new TaskRejectedException ("Executor [" + this .scheduledExecutor + "] did not accept task: " + task , ex );
@@ -151,8 +194,7 @@ public ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long p
151194
152195 public ScheduledFuture scheduleAtFixedRate (Runnable task , long period ) {
153196 try {
154- return this .scheduledExecutor .scheduleAtFixedRate (
155- errorHandlingTask (task , true ), 0 , period , TimeUnit .MILLISECONDS );
197+ return this .scheduledExecutor .scheduleAtFixedRate (decorateTask (task , true ), 0 , period , TimeUnit .MILLISECONDS );
156198 }
157199 catch (RejectedExecutionException ex ) {
158200 throw new TaskRejectedException ("Executor [" + this .scheduledExecutor + "] did not accept task: " + task , ex );
@@ -162,8 +204,7 @@ public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) {
162204 public ScheduledFuture scheduleWithFixedDelay (Runnable task , Date startTime , long delay ) {
163205 long initialDelay = startTime .getTime () - System .currentTimeMillis ();
164206 try {
165- return this .scheduledExecutor .scheduleWithFixedDelay (
166- errorHandlingTask (task , true ), initialDelay , delay , TimeUnit .MILLISECONDS );
207+ return this .scheduledExecutor .scheduleWithFixedDelay (decorateTask (task , true ), initialDelay , delay , TimeUnit .MILLISECONDS );
167208 }
168209 catch (RejectedExecutionException ex ) {
169210 throw new TaskRejectedException ("Executor [" + this .scheduledExecutor + "] did not accept task: " + task , ex );
@@ -172,16 +213,41 @@ public ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, lon
172213
173214 public ScheduledFuture scheduleWithFixedDelay (Runnable task , long delay ) {
174215 try {
175- return this .scheduledExecutor .scheduleWithFixedDelay (
176- errorHandlingTask (task , true ), 0 , delay , TimeUnit .MILLISECONDS );
216+ return this .scheduledExecutor .scheduleWithFixedDelay (decorateTask (task , true ), 0 , delay , TimeUnit .MILLISECONDS );
177217 }
178218 catch (RejectedExecutionException ex ) {
179219 throw new TaskRejectedException ("Executor [" + this .scheduledExecutor + "] did not accept task: " + task , ex );
180220 }
181221 }
182222
183- private Runnable errorHandlingTask (Runnable task , boolean isRepeatingTask ) {
184- return TaskUtils .decorateTaskWithErrorHandler (task , this .errorHandler , isRepeatingTask );
223+ private Runnable decorateTask (Runnable task , boolean isRepeatingTask ) {
224+ Runnable result = TaskUtils .decorateTaskWithErrorHandler (task , this .errorHandler , isRepeatingTask );
225+ if (this .enterpriseConcurrentScheduler ) {
226+ result = ManagedTaskBuilder .buildManagedTask (result , task .toString ());
227+ }
228+ return result ;
229+ }
230+
231+
232+ /**
233+ * Delegate that adapts a Spring Trigger to a JSR-236 Trigger.
234+ * Separated into an inner class in order to avoid a hard dependency on the JSR-236 API.
235+ */
236+ private class EnterpriseConcurrentTriggerScheduler {
237+
238+ public ScheduledFuture schedule (Runnable task , final Trigger trigger ) {
239+ ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService ) scheduledExecutor ;
240+ return executor .schedule (task , new javax .enterprise .concurrent .Trigger () {
241+ public Date getNextRunTime (LastExecution le , Date taskScheduledTime ) {
242+ return trigger .nextExecutionTime (le != null ?
243+ new SimpleTriggerContext (le .getScheduledStart (), le .getRunStart (), le .getRunEnd ()) :
244+ new SimpleTriggerContext ());
245+ }
246+ public boolean skipRun (LastExecution lastExecution , Date scheduledRunTime ) {
247+ return false ;
248+ }
249+ });
250+ }
185251 }
186252
187253}
0 commit comments