@@ -89,6 +89,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
89
89
}
90
90
91
91
92
+ @ Nullable
92
93
private ScheduledExecutorService scheduledExecutor ;
93
94
94
95
private boolean enterpriseConcurrentScheduler = false ;
@@ -168,6 +169,13 @@ public void setScheduledExecutor(ScheduledExecutorService scheduledExecutor) {
168
169
initScheduledExecutor (scheduledExecutor );
169
170
}
170
171
172
+ private ScheduledExecutorService getScheduledExecutor () {
173
+ if (this .scheduledExecutor == null ) {
174
+ throw new IllegalStateException ("No ScheduledExecutor is configured" );
175
+ }
176
+ return this .scheduledExecutor ;
177
+ }
178
+
171
179
/**
172
180
* Provide an {@link ErrorHandler} strategy.
173
181
*/
@@ -195,75 +203,81 @@ public Clock getClock() {
195
203
@ Override
196
204
@ Nullable
197
205
public ScheduledFuture <?> schedule (Runnable task , Trigger trigger ) {
206
+ ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor ();
198
207
try {
199
208
if (this .enterpriseConcurrentScheduler ) {
200
209
return new EnterpriseConcurrentTriggerScheduler ().schedule (decorateTask (task , true ), trigger );
201
210
}
202
211
else {
203
212
ErrorHandler errorHandler =
204
213
(this .errorHandler != null ? this .errorHandler : TaskUtils .getDefaultErrorHandler (true ));
205
- return new ReschedulingRunnable (task , trigger , this .clock , this . scheduledExecutor , errorHandler ).schedule ();
214
+ return new ReschedulingRunnable (task , trigger , this .clock , scheduleExecutorToUse , errorHandler ).schedule ();
206
215
}
207
216
}
208
217
catch (RejectedExecutionException ex ) {
209
- throw new TaskRejectedException (this . scheduledExecutor , task , ex );
218
+ throw new TaskRejectedException (scheduleExecutorToUse , task , ex );
210
219
}
211
220
}
212
221
213
222
@ Override
214
223
public ScheduledFuture <?> schedule (Runnable task , Instant startTime ) {
224
+ ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor ();
215
225
Duration delay = Duration .between (this .clock .instant (), startTime );
216
226
try {
217
- return this . scheduledExecutor .schedule (decorateTask (task , false ), NANO .convert (delay ), NANO );
227
+ return scheduleExecutorToUse .schedule (decorateTask (task , false ), NANO .convert (delay ), NANO );
218
228
}
219
229
catch (RejectedExecutionException ex ) {
220
- throw new TaskRejectedException (this . scheduledExecutor , task , ex );
230
+ throw new TaskRejectedException (scheduleExecutorToUse , task , ex );
221
231
}
222
232
}
223
233
224
234
@ Override
225
235
public ScheduledFuture <?> scheduleAtFixedRate (Runnable task , Instant startTime , Duration period ) {
236
+ ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor ();
226
237
Duration initialDelay = Duration .between (this .clock .instant (), startTime );
227
238
try {
228
- return this . scheduledExecutor .scheduleAtFixedRate (decorateTask (task , true ),
239
+ return scheduleExecutorToUse .scheduleAtFixedRate (decorateTask (task , true ),
229
240
NANO .convert (initialDelay ), NANO .convert (period ), NANO );
230
241
}
231
242
catch (RejectedExecutionException ex ) {
232
- throw new TaskRejectedException (this . scheduledExecutor , task , ex );
243
+ throw new TaskRejectedException (scheduleExecutorToUse , task , ex );
233
244
}
234
245
}
235
246
236
247
@ Override
237
248
public ScheduledFuture <?> scheduleAtFixedRate (Runnable task , Duration period ) {
249
+ ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor ();
238
250
try {
239
- return this . scheduledExecutor .scheduleAtFixedRate (decorateTask (task , true ),
251
+ return scheduleExecutorToUse .scheduleAtFixedRate (decorateTask (task , true ),
240
252
0 , NANO .convert (period ), NANO );
241
253
}
242
254
catch (RejectedExecutionException ex ) {
243
- throw new TaskRejectedException (this . scheduledExecutor , task , ex );
255
+ throw new TaskRejectedException (scheduleExecutorToUse , task , ex );
244
256
}
245
257
}
246
258
247
259
@ Override
248
260
public ScheduledFuture <?> scheduleWithFixedDelay (Runnable task , Instant startTime , Duration delay ) {
261
+ ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor ();
249
262
Duration initialDelay = Duration .between (this .clock .instant (), startTime );
250
263
try {
251
- return this . scheduledExecutor .scheduleWithFixedDelay (decorateTask (task , true ),
264
+ return scheduleExecutorToUse .scheduleWithFixedDelay (decorateTask (task , true ),
252
265
NANO .convert (initialDelay ), NANO .convert (delay ), NANO );
253
266
}
254
267
catch (RejectedExecutionException ex ) {
255
- throw new TaskRejectedException (this . scheduledExecutor , task , ex );
268
+ throw new TaskRejectedException (scheduleExecutorToUse , task , ex );
256
269
}
257
270
}
258
271
259
272
@ Override
260
273
public ScheduledFuture <?> scheduleWithFixedDelay (Runnable task , Duration delay ) {
274
+ ScheduledExecutorService scheduleExecutorToUse = getScheduledExecutor ();
261
275
try {
262
- return this . scheduledExecutor .scheduleWithFixedDelay (decorateTask (task , true ),
276
+ return scheduleExecutorToUse .scheduleWithFixedDelay (decorateTask (task , true ),
263
277
0 , NANO .convert (delay ), NANO );
264
278
}
265
279
catch (RejectedExecutionException ex ) {
266
- throw new TaskRejectedException (this . scheduledExecutor , task , ex );
280
+ throw new TaskRejectedException (scheduleExecutorToUse , task , ex );
267
281
}
268
282
}
269
283
@@ -283,7 +297,7 @@ private Runnable decorateTask(Runnable task, boolean isRepeatingTask) {
283
297
private class EnterpriseConcurrentTriggerScheduler {
284
298
285
299
public ScheduledFuture <?> schedule (Runnable task , Trigger trigger ) {
286
- ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService ) scheduledExecutor ;
300
+ ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService ) getScheduledExecutor () ;
287
301
return executor .schedule (task , new TriggerAdapter (trigger ));
288
302
}
289
303
0 commit comments