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

@@ -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);
}
}