Skip to content

Commit

Permalink
Expose shutdown state in TaskRejectedException message
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jul 7, 2023
1 parent ac11b03 commit 464b676
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
}
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.scheduledExecutor, task, ex);
}
}

Expand All @@ -217,7 +217,7 @@ public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
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);
}
}

Expand All @@ -229,7 +229,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime,
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);
}
}

Expand All @@ -240,7 +240,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
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);
}
}

Expand All @@ -252,7 +252,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTim
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);
}
}

Expand All @@ -263,7 +263,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void execute(Runnable task) {
executor.execute(task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(executor, task, ex);
}
}

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void execute(Runnable task) {
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);
}
}

Expand All @@ -301,7 +301,7 @@ public Future<?> submit(Runnable task) {
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);
}
}

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

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

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

Expand Down Expand Up @@ -379,7 +379,7 @@ public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
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);
}
}

Expand All @@ -391,7 +391,7 @@ public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
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);
}
}

Expand All @@ -404,7 +404,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime,
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);
}
}

Expand All @@ -416,7 +416,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
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);
}
}

Expand All @@ -429,7 +429,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTim
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);
}
}

Expand All @@ -441,7 +441,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +16,8 @@

package org.springframework.core.task;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;

/**
Expand Down Expand Up @@ -50,4 +52,26 @@ public TaskRejectedException(String msg, Throwable cause) {
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public void execute(Runnable task) {
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);
}
}

Expand All @@ -112,8 +111,7 @@ public Future<?> submit(Runnable task) {
}
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}

Expand All @@ -131,8 +129,7 @@ public <T> Future<T> submit(Callable<T> task) {
}
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}

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

Expand All @@ -157,8 +153,7 @@ public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
return future;
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException(
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
throw new TaskRejectedException(this.concurrentExecutor, task, ex);
}
}

Expand Down

0 comments on commit 464b676

Please sign in to comment.