Skip to content

Commit 464b676

Browse files
committed
Expose shutdown state in TaskRejectedException message
See gh-27090
1 parent ac11b03 commit 464b676

File tree

5 files changed

+52
-33
lines changed

5 files changed

+52
-33
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskScheduler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
206206
}
207207
}
208208
catch (RejectedExecutionException ex) {
209-
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
209+
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
210210
}
211211
}
212212

@@ -217,7 +217,7 @@ public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
217217
return this.scheduledExecutor.schedule(decorateTask(task, false), NANO.convert(delay), NANO);
218218
}
219219
catch (RejectedExecutionException ex) {
220-
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
220+
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
221221
}
222222
}
223223

@@ -229,7 +229,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime,
229229
NANO.convert(initialDelay), NANO.convert(period), NANO);
230230
}
231231
catch (RejectedExecutionException ex) {
232-
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
232+
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
233233
}
234234
}
235235

@@ -240,7 +240,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
240240
0, NANO.convert(period), NANO);
241241
}
242242
catch (RejectedExecutionException ex) {
243-
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
243+
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
244244
}
245245
}
246246

@@ -252,7 +252,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTim
252252
NANO.convert(initialDelay), NANO.convert(delay), NANO);
253253
}
254254
catch (RejectedExecutionException ex) {
255-
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
255+
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
256256
}
257257
}
258258

@@ -263,7 +263,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)
263263
0, NANO.convert(delay), NANO);
264264
}
265265
catch (RejectedExecutionException ex) {
266-
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
266+
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
267267
}
268268
}
269269

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public void execute(Runnable task) {
362362
executor.execute(task);
363363
}
364364
catch (RejectedExecutionException ex) {
365-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
365+
throw new TaskRejectedException(executor, task, ex);
366366
}
367367
}
368368

@@ -373,7 +373,7 @@ public Future<?> submit(Runnable task) {
373373
return executor.submit(task);
374374
}
375375
catch (RejectedExecutionException ex) {
376-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
376+
throw new TaskRejectedException(executor, task, ex);
377377
}
378378
}
379379

@@ -384,7 +384,7 @@ public <T> Future<T> submit(Callable<T> task) {
384384
return executor.submit(task);
385385
}
386386
catch (RejectedExecutionException ex) {
387-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
387+
throw new TaskRejectedException(executor, task, ex);
388388
}
389389
}
390390

@@ -397,7 +397,7 @@ public ListenableFuture<?> submitListenable(Runnable task) {
397397
return future;
398398
}
399399
catch (RejectedExecutionException ex) {
400-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
400+
throw new TaskRejectedException(executor, task, ex);
401401
}
402402
}
403403

@@ -410,7 +410,7 @@ public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
410410
return future;
411411
}
412412
catch (RejectedExecutionException ex) {
413-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
413+
throw new TaskRejectedException(executor, task, ex);
414414
}
415415
}
416416

spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskScheduler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public void execute(Runnable task) {
290290
executor.execute(errorHandlingTask(task, false));
291291
}
292292
catch (RejectedExecutionException ex) {
293-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
293+
throw new TaskRejectedException(executor, task, ex);
294294
}
295295
}
296296

@@ -301,7 +301,7 @@ public Future<?> submit(Runnable task) {
301301
return executor.submit(errorHandlingTask(task, false));
302302
}
303303
catch (RejectedExecutionException ex) {
304-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
304+
throw new TaskRejectedException(executor, task, ex);
305305
}
306306
}
307307

@@ -317,7 +317,7 @@ public <T> Future<T> submit(Callable<T> task) {
317317
return executor.submit(taskToUse);
318318
}
319319
catch (RejectedExecutionException ex) {
320-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
320+
throw new TaskRejectedException(executor, task, ex);
321321
}
322322
}
323323

@@ -330,7 +330,7 @@ public ListenableFuture<?> submitListenable(Runnable task) {
330330
return listenableFuture;
331331
}
332332
catch (RejectedExecutionException ex) {
333-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
333+
throw new TaskRejectedException(executor, task, ex);
334334
}
335335
}
336336

@@ -343,7 +343,7 @@ public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
343343
return listenableFuture;
344344
}
345345
catch (RejectedExecutionException ex) {
346-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
346+
throw new TaskRejectedException(executor, task, ex);
347347
}
348348
}
349349

@@ -379,7 +379,7 @@ public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
379379
return new ReschedulingRunnable(task, trigger, this.clock, executor, errorHandler).schedule();
380380
}
381381
catch (RejectedExecutionException ex) {
382-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
382+
throw new TaskRejectedException(executor, task, ex);
383383
}
384384
}
385385

@@ -391,7 +391,7 @@ public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
391391
return executor.schedule(errorHandlingTask(task, false), NANO.convert(delay), NANO);
392392
}
393393
catch (RejectedExecutionException ex) {
394-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
394+
throw new TaskRejectedException(executor, task, ex);
395395
}
396396
}
397397

@@ -404,7 +404,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime,
404404
NANO.convert(initialDelay), NANO.convert(period), NANO);
405405
}
406406
catch (RejectedExecutionException ex) {
407-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
407+
throw new TaskRejectedException(executor, task, ex);
408408
}
409409
}
410410

@@ -416,7 +416,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
416416
0, NANO.convert(period), NANO);
417417
}
418418
catch (RejectedExecutionException ex) {
419-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
419+
throw new TaskRejectedException(executor, task, ex);
420420
}
421421
}
422422

@@ -429,7 +429,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTim
429429
NANO.convert(initialDelay), NANO.convert(delay), NANO);
430430
}
431431
catch (RejectedExecutionException ex) {
432-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
432+
throw new TaskRejectedException(executor, task, ex);
433433
}
434434
}
435435

@@ -441,7 +441,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)
441441
0, NANO.convert(delay), NANO);
442442
}
443443
catch (RejectedExecutionException ex) {
444-
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
444+
throw new TaskRejectedException(executor, task, ex);
445445
}
446446
}
447447

spring-core/src/main/java/org/springframework/core/task/TaskRejectedException.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.core.task;
1818

19+
import java.util.concurrent.Executor;
20+
import java.util.concurrent.ExecutorService;
1921
import java.util.concurrent.RejectedExecutionException;
2022

2123
/**
@@ -50,4 +52,26 @@ public TaskRejectedException(String msg, Throwable cause) {
5052
super(msg, cause);
5153
}
5254

55+
/**
56+
* Create a new {@code TaskRejectedException}
57+
* with a default message for the given executor and task.
58+
* @param executor the {@code Executor} that rejected the task
59+
* @param task the task object that got rejected
60+
* @param cause the original {@link RejectedExecutionException}
61+
* @since 6.1
62+
* @see ExecutorService#isShutdown()
63+
* @see java.util.concurrent.RejectedExecutionException
64+
*/
65+
public TaskRejectedException(Executor executor, Object task, RejectedExecutionException cause) {
66+
super(executorDescription(executor) + " did not accept task: " + task, cause);
67+
}
68+
69+
70+
private static String executorDescription(Executor executor) {
71+
if (executor instanceof ExecutorService executorService) {
72+
return "ExecutorService in " + (executorService.isShutdown() ? "shutdown" : "active") + " state";
73+
}
74+
return executor.toString();
75+
}
76+
5377
}

spring-core/src/main/java/org/springframework/core/task/support/TaskExecutorAdapter.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public void execute(Runnable task) {
9393
doExecute(this.concurrentExecutor, this.taskDecorator, task);
9494
}
9595
catch (RejectedExecutionException ex) {
96-
throw new TaskRejectedException(
97-
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
96+
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
9897
}
9998
}
10099

@@ -112,8 +111,7 @@ public Future<?> submit(Runnable task) {
112111
}
113112
}
114113
catch (RejectedExecutionException ex) {
115-
throw new TaskRejectedException(
116-
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
114+
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
117115
}
118116
}
119117

@@ -131,8 +129,7 @@ public <T> Future<T> submit(Callable<T> task) {
131129
}
132130
}
133131
catch (RejectedExecutionException ex) {
134-
throw new TaskRejectedException(
135-
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
132+
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
136133
}
137134
}
138135

@@ -144,8 +141,7 @@ public ListenableFuture<?> submitListenable(Runnable task) {
144141
return future;
145142
}
146143
catch (RejectedExecutionException ex) {
147-
throw new TaskRejectedException(
148-
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
144+
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
149145
}
150146
}
151147

@@ -157,8 +153,7 @@ public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
157153
return future;
158154
}
159155
catch (RejectedExecutionException ex) {
160-
throw new TaskRejectedException(
161-
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
156+
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
162157
}
163158
}
164159

0 commit comments

Comments
 (0)