Skip to content

Commit 3d4d68c

Browse files
committed
Run listener/send task locally as fallback on RejectedExecutionException
Closes gh-32171
1 parent b61552b commit 3d4d68c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Diff for: spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.context.event;
1818

1919
import java.util.concurrent.Executor;
20+
import java.util.concurrent.RejectedExecutionException;
2021

2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
@@ -143,7 +144,13 @@ public void multicastEvent(ApplicationEvent event, @Nullable ResolvableType even
143144
Executor executor = getTaskExecutor();
144145
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
145146
if (executor != null && listener.supportsAsyncExecution()) {
146-
executor.execute(() -> invokeListener(listener, event));
147+
try {
148+
executor.execute(() -> invokeListener(listener, event));
149+
}
150+
catch (RejectedExecutionException ex) {
151+
// Probably on shutdown -> invoke listener locally instead
152+
invokeListener(listener, event);
153+
}
147154
}
148155
else {
149156
invokeListener(listener, event);

Diff for: spring-messaging/src/main/java/org/springframework/messaging/support/ExecutorSubscribableChannel.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.concurrent.Executor;
22+
import java.util.concurrent.RejectedExecutionException;
2223

2324
import org.springframework.lang.Nullable;
2425
import org.springframework.messaging.Message;
@@ -96,11 +97,18 @@ private void updateExecutorInterceptorsFor(ChannelInterceptor interceptor) {
9697
public boolean sendInternal(Message<?> message, long timeout) {
9798
for (MessageHandler handler : getSubscribers()) {
9899
SendTask sendTask = new SendTask(message, handler);
99-
if (this.executor == null) {
100-
sendTask.run();
100+
if (this.executor != null) {
101+
try {
102+
this.executor.execute(sendTask);
103+
}
104+
catch (RejectedExecutionException ex) {
105+
// Probably on shutdown -> run send task locally instead
106+
sendTask.run();
107+
}
101108
}
102109
else {
103-
this.executor.execute(sendTask);
110+
// No executor configured -> always run send tasks locally
111+
sendTask.run();
104112
}
105113
}
106114
return true;

0 commit comments

Comments
 (0)