Expose shutdown state in TaskRejectedException message

See gh-27090
This commit is contained in:
Juergen Hoeller 2023-07-07 23:59:10 +02:00
parent ac11b03cd3
commit 464b676ec5
5 changed files with 52 additions and 33 deletions

View File

@ -206,7 +206,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}
@ -217,7 +217,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
return this.scheduledExecutor.schedule(decorateTask(task, false), NANO.convert(delay), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}
@ -229,7 +229,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
NANO.convert(initialDelay), NANO.convert(period), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}
@ -240,7 +240,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
0, NANO.convert(period), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}
@ -252,7 +252,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
NANO.convert(initialDelay), NANO.convert(delay), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}
@ -263,7 +263,7 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
0, NANO.convert(delay), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}

View File

@ -362,7 +362,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
executor.execute(task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -373,7 +373,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
return executor.submit(task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -384,7 +384,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
return executor.submit(task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -397,7 +397,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
return future;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -410,7 +410,7 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
return future;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}

View File

@ -290,7 +290,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
executor.execute(errorHandlingTask(task, false));
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -301,7 +301,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
return executor.submit(errorHandlingTask(task, false));
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -317,7 +317,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
return executor.submit(taskToUse);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -330,7 +330,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
return listenableFuture;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -343,7 +343,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
return listenableFuture;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -379,7 +379,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
return new ReschedulingRunnable(task, trigger, this.clock, executor, errorHandler).schedule();
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -391,7 +391,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
return executor.schedule(errorHandlingTask(task, false), NANO.convert(delay), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -404,7 +404,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
NANO.convert(initialDelay), NANO.convert(period), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -416,7 +416,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
0, NANO.convert(period), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -429,7 +429,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
NANO.convert(initialDelay), NANO.convert(delay), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}
@ -441,7 +441,7 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
0, NANO.convert(delay), NANO);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.core.task;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
/**
@ -50,4 +52,26 @@ public class TaskRejectedException extends RejectedExecutionException {
super(msg, cause);
}
/**
* Create a new {@code TaskRejectedException}
* with a default message for the given executor and task.
* @param executor the {@code Executor} that rejected the task
* @param task the task object that got rejected
* @param cause the original {@link RejectedExecutionException}
* @since 6.1
* @see ExecutorService#isShutdown()
* @see java.util.concurrent.RejectedExecutionException
*/
public TaskRejectedException(Executor executor, Object task, RejectedExecutionException cause) {
super(executorDescription(executor) + " did not accept task: " + task, cause);
}
private static String executorDescription(Executor executor) {
if (executor instanceof ExecutorService executorService) {
return "ExecutorService in " + (executorService.isShutdown() ? "shutdown" : "active") + " state";
}
return executor.toString();
}
}

View File

@ -93,8 +93,7 @@ public class TaskExecutorAdapter implements AsyncListenableTaskExecutor {
doExecute(this.concurrentExecutor, this.taskDecorator, task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}
@ -112,8 +111,7 @@ public class TaskExecutorAdapter implements AsyncListenableTaskExecutor {
}
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}
@ -131,8 +129,7 @@ public class TaskExecutorAdapter implements AsyncListenableTaskExecutor {
}
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}
@ -144,8 +141,7 @@ public class TaskExecutorAdapter implements AsyncListenableTaskExecutor {
return future;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}
@ -157,8 +153,7 @@ public class TaskExecutorAdapter implements AsyncListenableTaskExecutor {
return future;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}