Skip to content

When virtual threads are enabled, configure Spring Integration's task scheduler to use them #41188

Closed as not planned
@csh0034

Description

@csh0034

In the PR below, WebSocketMessagingAutoConfiguration has been modified to use a virtual thread executor

#39611

public class WebSocketMessagingAutoConfiguration {
  // ...
  WebSocketMessageConverterConfiguration(ObjectMapper objectMapper,
    Map<String, AsyncTaskExecutor> taskExecutors) {
    this.objectMapper = objectMapper;
    this.executor = determineAsyncTaskExecutor(taskExecutors);
  }

  private static AsyncTaskExecutor determineAsyncTaskExecutor(Map<String, AsyncTaskExecutor> taskExecutors) {
    if (taskExecutors.size() == 1) {
      return taskExecutors.values().iterator().next();
    }
    return taskExecutors.get(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME);
  }

  // ...
}

Upon checking the taskExecutors being injected, it is taskScheduler.
When used with spring-integration, the following Bean is being configured first.
Is it possible to modify it to allow for auto-configuration?

@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(TaskSchedulerBuilder.class)
@ConditionalOnMissingBean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
@SuppressWarnings("removal")
public class IntegrationAutoConfiguration {
  // ..
  protected static class IntegrationTaskSchedulerConfiguration {
    @Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
    public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder taskSchedulerBuilder,
      ObjectProvider<ThreadPoolTaskSchedulerBuilder> threadPoolTaskSchedulerBuilderProvider) {
      // ...
    }
  }
  // ..
}

Below is the configuration added without using auto-configuration.

@Configuration
class ExecutorConfig {
  @Bean(
    name = [
      TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME,
      AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME
    ]
  )
  fun applicationTaskExecutorVirtualThreads(builder: SimpleAsyncTaskExecutorBuilder): SimpleAsyncTaskExecutor {
    return builder.build()
  }
}

Of course, the bottom properties are also set.

spring:
  threads:
    virtual:
      enabled: true

Metadata

Metadata

Assignees

Labels

status: supersededAn issue that has been superseded by another

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions