Skip to content

Commit 521bbfc

Browse files
committed
Allow configuring custom ThreadPoolTaskExecutor
Issue: SPR-12272
1 parent 179b236 commit 521bbfc

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/config/ChannelRegistration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.messaging.support.ChannelInterceptor;
24+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
2425

2526
/**
2627
* A registration class for customizing the configuration for a
@@ -46,6 +47,17 @@ public TaskExecutorRegistration taskExecutor() {
4647
return this.registration;
4748
}
4849

50+
/**
51+
* Configure the thread pool backing this message channel using a custom
52+
* ThreadPoolTaskExecutor.
53+
*/
54+
public TaskExecutorRegistration taskExecutor(ThreadPoolTaskExecutor taskExecutor) {
55+
if (this.registration == null) {
56+
this.registration = new TaskExecutorRegistration(taskExecutor);
57+
}
58+
return this.registration;
59+
}
60+
4961
/**
5062
* Configure interceptors for the message channel.
5163
*/

spring-messaging/src/main/java/org/springframework/messaging/simp/config/TaskExecutorRegistration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
public class TaskExecutorRegistration {
2828

29+
private ThreadPoolTaskExecutor taskExecutor;
30+
2931
private int corePoolSize = Runtime.getRuntime().availableProcessors() * 2;
3032

3133
private int maxPoolSize = Integer.MAX_VALUE;
@@ -35,6 +37,13 @@ public class TaskExecutorRegistration {
3537
private int keepAliveSeconds = 60;
3638

3739

40+
public TaskExecutorRegistration() {
41+
}
42+
43+
public TaskExecutorRegistration(ThreadPoolTaskExecutor taskExecutor) {
44+
this.taskExecutor = taskExecutor;
45+
}
46+
3847
/**
3948
* Set the core pool size of the ThreadPoolExecutor.
4049
* <p><strong>NOTE:</strong> The core pool size is effectively the max pool size
@@ -93,7 +102,7 @@ public TaskExecutorRegistration keepAliveSeconds(int keepAliveSeconds) {
93102
}
94103

95104
protected ThreadPoolTaskExecutor getTaskExecutor() {
96-
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
105+
ThreadPoolTaskExecutor executor = (this.taskExecutor != null ? this.taskExecutor : new ThreadPoolTaskExecutor());
97106
executor.setCorePoolSize(this.corePoolSize);
98107
executor.setMaxPoolSize(this.maxPoolSize);
99108
executor.setKeepAliveSeconds(this.keepAliveSeconds);

spring-messaging/src/main/java/org/springframework/messaging/support/Test.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public void clientInboundChannelCustomized() {
132132

133133
assertEquals(2, channel.getInterceptors().size());
134134

135-
ThreadPoolTaskExecutor taskExecutor = this.customContext.getBean(
136-
"clientInboundChannelExecutor", ThreadPoolTaskExecutor.class);
135+
CustomThreadPoolTaskExecutor taskExecutor = this.customContext.getBean(
136+
"clientInboundChannelExecutor", CustomThreadPoolTaskExecutor.class);
137137

138138
assertEquals(11, taskExecutor.getCorePoolSize());
139139
assertEquals(12, taskExecutor.getMaxPoolSize());
@@ -489,7 +489,8 @@ static class CustomConfig extends AbstractMessageBrokerConfiguration {
489489
@Override
490490
protected void configureClientInboundChannel(ChannelRegistration registration) {
491491
registration.setInterceptors(this.interceptor);
492-
registration.taskExecutor().corePoolSize(11).maxPoolSize(12).keepAliveSeconds(13).queueCapacity(14);
492+
registration.taskExecutor(new CustomThreadPoolTaskExecutor())
493+
.corePoolSize(11).maxPoolSize(12).keepAliveSeconds(13).queueCapacity(14);
493494
}
494495

495496
@Override
@@ -540,4 +541,7 @@ public void validate(Object target, Errors errors) {
540541
}
541542
}
542543

544+
private static class CustomThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
545+
}
546+
543547
}

0 commit comments

Comments
 (0)