Skip to content

Commit 5e141ab

Browse files
fix: Fail on multiple executors only when the executor is accessed (#21839) (#21843)
Does not fail eagerly if a Spring app has multiple TaskExecutor bean (without VaadinTaskExecutor qualifier). Instead it fails when an app tries to access VaadinService.getExecutor() directly or via using a feature that uses this executor (currently the signals preview). Fixes #21811 Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
1 parent bc3b7ad commit 5e141ab

4 files changed

Lines changed: 55 additions & 26 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ private void initSignalsEnvironment() {
362362
FeatureFlags featureFlags = FeatureFlags.get(getContext());
363363
if (featureFlags
364364
.isEnabled(FeatureFlags.FLOW_FULLSTACK_SIGNALS.getId())) {
365-
signalsExecutor = this.executor;
365+
// Use getter method to trigger a multiple TaskExecutor check
366+
signalsExecutor = getExecutor();
366367
flowDispatcherOverride = () -> {
367368
UI owner = UI.getCurrent();
368369
if (owner == null) {

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringVaadinServletService.java‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class SpringVaadinServletService extends VaadinServletService {
5454

5555
static final String SPRING_BOOT_WEBPROPERTIES_CLASS = "org.springframework.boot.autoconfigure.web.WebProperties";
5656

57+
private Set<String> multipleExecutorCandidates;
58+
5759
/**
5860
* Creates an instance connected to the given servlet and using the given
5961
* configuration with provided application {@code context}.
@@ -149,22 +151,29 @@ protected Executor createDefaultExecutor() {
149151
// bean
150152
candidates.remove("applicationTaskExecutor");
151153
}
152-
if (candidates.size() == 1) {
153-
return context.getBean(candidates.iterator().next(),
154-
TaskExecutor.class);
154+
if (candidates.size() > 1) {
155+
multipleExecutorCandidates = candidates;
155156
}
157+
return context.getBean(candidates.iterator().next(),
158+
TaskExecutor.class);
159+
}
156160

157-
String message = String.format("Multiple TaskExecutor beans found: %s. "
158-
+ "Please resolve this conflict by either: "
159-
+ "(1) Providing a single TaskExecutor bean, or "
160-
+ "(2) Marking the bean to use with Vaadin by: "
161-
+ "naming it '%s' (e.g. @Bean(\"%s\")), or "
162-
+ "applying the @%s qualifier annotation to the bean definition. "
163-
+ "Note: To prevent an Executor bean from replacing the default one "
164-
+ "provided by Spring, consider setting '@Bean(defaultCandidate=false)' on its definition.",
165-
candidates, VaadinTaskExecutor.NAME, VaadinTaskExecutor.NAME,
166-
VaadinTaskExecutor.class.getSimpleName());
167-
throw new IllegalStateException(message);
161+
@Override
162+
public Executor getExecutor() {
163+
if (multipleExecutorCandidates != null) {
164+
String message = String.format(
165+
"Multiple TaskExecutor beans found: %s. "
166+
+ "Please resolve this conflict by either: "
167+
+ "(1) Providing a single TaskExecutor bean, or "
168+
+ "(2) Marking the bean to use with Vaadin by: "
169+
+ "naming it '%s' (e.g. @Bean(\"%s\")), or "
170+
+ "applying the @%s qualifier annotation to the bean definition.",
171+
multipleExecutorCandidates, VaadinTaskExecutor.NAME,
172+
VaadinTaskExecutor.NAME,
173+
VaadinTaskExecutor.class.getSimpleName());
174+
throw new IllegalStateException(message);
175+
}
176+
return super.getExecutor();
168177
}
169178

170179
@Override

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/annotation/VaadinTaskExecutor.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646
* <li>Both an annotated bean and a bean with the name
4747
* {@code VaadinTaskExecutor} cannot exist simultaneously.</li>
4848
* </ul>
49-
* <p>
50-
* To prevent a custom executor from replacing the default one, consider setting
51-
* {@code @Bean(defaultCandidate=false)} on your bean definition.
5249
*/
5350
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
5451
ElementType.TYPE, ElementType.ANNOTATION_TYPE })

‎vaadin-spring/src/test/java/com/vaadin/flow/spring/service/SpringVaadinServiceExecutorTest.java‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,15 @@ public void getExecutor_customExecutorViaInitListener_returnsCustomTaskExecutor(
391391
}
392392

393393
@Test
394-
public void init_multipleUnnamedTaskExecutors_throws() {
394+
public void init_multipleUnnamedTaskExecutors_executorAccessed_throws() {
395395
contextRunner.withUserConfiguration(MultipleExecutorsConfig.class)
396396
.run(context -> {
397397
IllegalStateException error = assertThrows(
398-
IllegalStateException.class,
399-
() -> SpringInstantiatorTest.getService(context,
400-
new Properties()));
398+
IllegalStateException.class, () -> {
399+
VaadinService service = SpringInstantiatorTest
400+
.getService(context, new Properties());
401+
Executor executor = service.getExecutor();
402+
});
401403
assertTrue(error.getMessage()
402404
.contains("Multiple TaskExecutor beans found"));
403405
assertTrue(error.getMessage()
@@ -408,13 +410,33 @@ public void init_multipleUnnamedTaskExecutors_throws() {
408410
}
409411

410412
@Test
411-
public void init_multipleNamedTaskExecutors_throws() {
413+
public void init_multipleUnnamedTaskExecutors_executorNotAccessed_doesntThrow() {
414+
contextRunner.withUserConfiguration(MultipleExecutorsConfig.class)
415+
.run(context -> {
416+
SpringInstantiatorTest.getService(context,
417+
new Properties());
418+
});
419+
}
420+
421+
@Test
422+
public void init_multipleNamedTaskExecutors_executorNotAccessed_doesnThrow() {
423+
contextRunner.withUserConfiguration(MultipleNamedExecutorsConfig.class)
424+
.run(context -> {
425+
SpringInstantiatorTest.getService(context,
426+
new Properties());
427+
});
428+
}
429+
430+
@Test
431+
public void init_multipleNamedTaskExecutors_executorAccessed_throws() {
412432
contextRunner.withUserConfiguration(MultipleNamedExecutorsConfig.class)
413433
.run(context -> {
414434
IllegalStateException error = assertThrows(
415-
IllegalStateException.class,
416-
() -> SpringInstantiatorTest.getService(context,
417-
new Properties()));
435+
IllegalStateException.class, () -> {
436+
VaadinService service = SpringInstantiatorTest
437+
.getService(context, new Properties());
438+
Executor executor = service.getExecutor();
439+
});
418440
assertTrue(error.getMessage()
419441
.contains("Multiple TaskExecutor beans found"));
420442
assertTrue(error.getMessage()

0 commit comments

Comments
 (0)