Skip to content

Commit 9110b12

Browse files
committed
Polish "Configure suitable TaskExecutor for WebSocket"
See gh-39611
1 parent 1d820a8 commit 9110b12

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -67,7 +67,6 @@
6767
* @author Josh Long
6868
* @author Scott Frederick
6969
* @author Stefano Cordio
70-
* @author Lasse Wulff
7170
* @since 1.0.0
7271
* @see EnableJpaRepositories
7372
*/
@@ -85,14 +84,20 @@ public class JpaRepositoriesAutoConfiguration {
8584
public EntityManagerFactoryBuilderCustomizer entityManagerFactoryBootstrapExecutorCustomizer(
8685
Map<String, AsyncTaskExecutor> taskExecutors) {
8786
return (builder) -> {
88-
AsyncTaskExecutor bootstrapExecutor = TaskExecutionAutoConfiguration
89-
.determineAsyncTaskExecutor(taskExecutors);
87+
AsyncTaskExecutor bootstrapExecutor = determineBootstrapExecutor(taskExecutors);
9088
if (bootstrapExecutor != null) {
9189
builder.setBootstrapExecutor(bootstrapExecutor);
9290
}
9391
};
9492
}
9593

94+
private AsyncTaskExecutor determineBootstrapExecutor(Map<String, AsyncTaskExecutor> taskExecutors) {
95+
if (taskExecutors.size() == 1) {
96+
return taskExecutors.values().iterator().next();
97+
}
98+
return taskExecutors.get(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME);
99+
}
100+
96101
private static final class BootstrapExecutorCondition extends AnyNestedCondition {
97102

98103
BootstrapExecutorCondition() {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -16,14 +16,11 @@
1616

1717
package org.springframework.boot.autoconfigure.task;
1818

19-
import java.util.Map;
20-
2119
import org.springframework.boot.autoconfigure.AutoConfiguration;
2220
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2321
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2422
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2523
import org.springframework.context.annotation.Import;
26-
import org.springframework.core.task.AsyncTaskExecutor;
2724
import org.springframework.core.task.TaskExecutor;
2825
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
2926

@@ -33,7 +30,6 @@
3330
* @author Stephane Nicoll
3431
* @author Camille Vienot
3532
* @author Moritz Halbritter
36-
* @author Lasse Wulff
3733
* @since 2.1.0
3834
*/
3935
@ConditionalOnClass(ThreadPoolTaskExecutor.class)
@@ -50,11 +46,4 @@ public class TaskExecutionAutoConfiguration {
5046
*/
5147
public static final String APPLICATION_TASK_EXECUTOR_BEAN_NAME = "applicationTaskExecutor";
5248

53-
public static AsyncTaskExecutor determineAsyncTaskExecutor(Map<String, AsyncTaskExecutor> taskExecutors) {
54-
if (taskExecutors.size() == 1) {
55-
return taskExecutors.values().iterator().next();
56-
}
57-
return taskExecutors.get(APPLICATION_TASK_EXECUTOR_BEAN_NAME);
58-
}
59-
6049
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketMessagingAutoConfiguration.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
*
5050
* @author Andy Wilkinson
5151
* @author Lasse Wulff
52+
* @author Moritz Halbritter
5253
* @since 1.3.0
5354
*/
5455
@AutoConfiguration(after = JacksonAutoConfiguration.class)
@@ -68,7 +69,14 @@ static class WebSocketMessageConverterConfiguration implements WebSocketMessageB
6869
WebSocketMessageConverterConfiguration(ObjectMapper objectMapper,
6970
Map<String, AsyncTaskExecutor> taskExecutors) {
7071
this.objectMapper = objectMapper;
71-
this.executor = TaskExecutionAutoConfiguration.determineAsyncTaskExecutor(taskExecutors);
72+
this.executor = determineAsyncTaskExecutor(taskExecutors);
73+
}
74+
75+
private static AsyncTaskExecutor determineAsyncTaskExecutor(Map<String, AsyncTaskExecutor> taskExecutors) {
76+
if (taskExecutors.size() == 1) {
77+
return taskExecutors.values().iterator().next();
78+
}
79+
return taskExecutors.get(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME);
7280
}
7381

7482
@Override
@@ -85,12 +93,16 @@ public boolean configureMessageConverters(List<MessageConverter> messageConverte
8593

8694
@Override
8795
public void configureClientInboundChannel(ChannelRegistration registration) {
88-
registration.executor(this.executor);
96+
if (this.executor != null) {
97+
registration.executor(this.executor);
98+
}
8999
}
90100

91101
@Override
92102
public void configureClientOutboundChannel(ChannelRegistration registration) {
93-
registration.executor(this.executor);
103+
if (this.executor != null) {
104+
registration.executor(this.executor);
105+
}
94106
}
95107

96108
@Bean

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/servlet/WebSocketMessagingAutoConfigurationTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.context.annotation.Configuration;
4949
import org.springframework.core.task.AsyncTaskExecutor;
5050
import org.springframework.core.task.SimpleAsyncTaskExecutor;
51+
import org.springframework.core.task.TaskExecutor;
5152
import org.springframework.messaging.converter.CompositeMessageConverter;
5253
import org.springframework.messaging.converter.MessageConverter;
5354
import org.springframework.messaging.converter.SimpleMessageConverter;
@@ -144,11 +145,9 @@ void predefinedThreadExecutorIsSelectedForInboundChannel() throws Throwable {
144145
WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration configuration = new WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration(
145146
new ObjectMapper(),
146147
Map.of(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME, expectedExecutor));
147-
148148
configuration.configureClientInboundChannel(registration);
149-
150-
AsyncTaskExecutor mappedExecutor = (AsyncTaskExecutor) FieldUtils.getFieldValue(registration, "executor");
151-
assertThat(mappedExecutor).isEqualTo(expectedExecutor);
149+
TaskExecutor executor = (TaskExecutor) FieldUtils.getFieldValue(registration, "executor");
150+
assertThat(executor).isEqualTo(expectedExecutor);
152151
}
153152

154153
@Test
@@ -158,11 +157,9 @@ void predefinedThreadExecutorIsSelectedForOutboundChannel() throws Throwable {
158157
WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration configuration = new WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration(
159158
new ObjectMapper(),
160159
Map.of(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME, expectedExecutor));
161-
162160
configuration.configureClientOutboundChannel(registration);
163-
164-
AsyncTaskExecutor mappedExecutor = (AsyncTaskExecutor) FieldUtils.getFieldValue(registration, "executor");
165-
assertThat(mappedExecutor).isEqualTo(expectedExecutor);
161+
TaskExecutor executor = (TaskExecutor) FieldUtils.getFieldValue(registration, "executor");
162+
assertThat(executor).isEqualTo(expectedExecutor);
166163
}
167164

168165
private List<MessageConverter> getCustomizedConverters() {

0 commit comments

Comments
 (0)