Skip to content

Ease the creation of multiple task executor and task scheduled builders #39928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
frosiere opened this issue Mar 13, 2024 · 5 comments
Open
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement

Comments

@frosiere
Copy link
Contributor

frosiere commented Mar 13, 2024

Through an auto-configuration, Spring Boot makes it easy to create a task executor builder and a task scheduled builders with their respective sets of properties.

In most cases, this is sufficient, but in some specific cases, it may be necessary to create multiple builders with multiple sets of properties. In this case, the configuration has to be defined manually, resulting in some duplication of Spring Boot code.

It would be great if Spring Boot could provide an easy way to create these builders from a given property class.

Let's take a concrete example. Suppose an application needs to create 2 ThreadPoolTaskSchedulerBuilder with 2 sets of properties.
The support could be given as follows

TaskSchedulingProperties taskSchedulingProperties1 = new TaskSchedulingProperties();
ThreadPoolTaskSchedulerBuilder taskSchedulerBuilder1 = xxx.create(taskSchedulingProperties);

TaskSchedulingProperties taskSchedulingProperties2 = new TaskSchedulingProperties();
ThreadPoolTaskSchedulerBuilder taskSchedulerBuilder2 = xxx.create(taskSchedulingProperties);

xxx could either be a factory, a utility class or a static method on the property class itself.

Our use case is to create several builders in different contexts such as grpc, kafka, etc.

In previous versions of Spring boot, it was possible to abuse the use of auto-configurations as follows

    TaskExecutorBuilder createTaskExecutorBuilder(TaskExecutionProperties properties) {
        return new TaskExecutionAutoConfiguration().taskExecutorBuilder(
                properties, 
                new EmptyObjectProvider<>(), 
                new EmptyObjectProvider<>()
        );
    }
    TaskSchedulerBuilder createTaskSchedulerBuilder(TaskSchedulingProperties properties) {
        return new TaskSchedulingAutoConfiguration().taskSchedulerBuilder(
                properties, 
                new EmptyObjectProvider<>()
        );
    }

Any feedback or comments are more than welcome.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 13, 2024
@mhalbritter
Copy link
Contributor

Just FYI: we don't consider the auto-configurations public API (only their names), so something like this:

 return new TaskExecutionAutoConfiguration().taskExecutorBuilder(

is absolutely not recommended.

@frosiere
Copy link
Contributor Author

frosiere commented Mar 13, 2024

Just FYI: we don't consider the auto-configurations public API (only their names), so something like this:

 return new TaskExecutionAutoConfiguration().taskExecutorBuilder(

is absolutely not recommended.

Thanks for the quick reply. Fully agree, it's an abusive usage of an internal API (just gave it as an "example").
That's why I created this issue to achieve our use case in a nice way without having to copy code.

@scottfrederick
Copy link
Contributor

We've had several issues in the past that are similar to this one, in that they express a desire to make it easier to create multiple beans of a given type without having to duplicate Spring Boot's auto-configuration code. #22403 and #35157 are examples. #21322 has been referenced as a possible solution for cases where the beans are connections to external services, but that would not apply to requirements like the one in this issue. So, this feels like a duplicate but I'm not sure any of the existing issues captures the general requirements.

@frosiere
Copy link
Contributor Author

Thank you for your reply. I checked the other issues, the use case seems similar but not identical, and these issues are also quite old. It seems difficult to have a general approach that works in all cases.

So, couldn't we try to apply the following approach: if a complex object can directly be built from a configuration properties class, wouldn't it make sense to expose a factory method on that class?

Concrete example

        TaskExecutionProperties taskExecutionProperties = new TaskExecutionProperties();
        TaskExecutorBuilder taskExecutorBuilder = taskExecutionProperties.create();

Another approach might be to use a configurer or a customizer, but I'm not sure that's the best fit.

@philwebb philwebb added the for: team-meeting An issue we'd like to discuss as a team to make progress label Apr 22, 2024
@philwebb philwebb added type: enhancement A general enhancement status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged for: team-meeting An issue we'd like to discuss as a team to make progress labels May 1, 2024
@philwebb philwebb added this to the General Backlog milestone May 1, 2024
@philwebb
Copy link
Member

philwebb commented May 1, 2024

We discussed this today and we think this is a general problem that we need to invest some time designing. The general pattern of creating an object from properties comes up again and again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants