Skip to content
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

Make it easier to execute a method on a particular type of thread #31212

Closed
wilkinsona opened this issue Sep 13, 2023 · 1 comment
Closed

Make it easier to execute a method on a particular type of thread #31212

wilkinsona opened this issue Sep 13, 2023 · 1 comment
Labels
for: external-project Needs a fix in external project in: messaging Issues in messaging modules (jms, messaging) in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement

Comments

@wilkinsona
Copy link
Member

Affects: 6.1

With the introduction of virtual thread support, I anticipate that there will be an increased need for executing a method on a particular type of thread. Specifically, when a Servlet web server is using virtual threads for request handling, I anticipate a need to switch execution to a platform thread to avoid pinning of virtual threads caused by unavoidable synchronization in third-party code. This Tomcat mailing list discussion is one example of this problem.

This can be achieved today using an @Async annotated method that references a TaskExecutor that is using platform threads but it comes with a reasonably large amount of boiler plate as you have to move code into a separate method and work with the Future API.

To simplify things, I would like Framework to deal with the Future on my behalf so that I can write a "normal" method and have it automatically run on a thread from a particular task executor. There's prior art for this sort of functionality in Micronaut with its @ExecuteOn annotation. In a Spring app, I envisage that a similar annotation could be used on any method that's an entry point into the application such as a method on a @Controller.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 13, 2023
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Sep 13, 2023
@jhoeller jhoeller removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 14, 2023
@jhoeller jhoeller added this to the 6.1.x milestone Sep 14, 2023
@jhoeller jhoeller added in: messaging Issues in messaging modules (jms, messaging) in: web Issues in web modules (web, webmvc, webflux, websocket) and removed in: core Issues in core modules (aop, beans, core, context, expression) labels Sep 14, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.x Backlog Nov 9, 2023
@jhoeller jhoeller modified the milestones: 6.x Backlog, General Backlog Oct 1, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Nov 20, 2024

After an internal discussion, it would not be straight forward to implement this for Spring MVC if at all. In the very least it would require starting an async request, which is not expected here, and it might not be feasible to implement and support all controller method return types.

It is much easier to do this on WebFlux where switching threads can done seamlessly. On WebFlux, the benefit would be to get off the event loop and onto a thread pool for blocking. Note however that WebFlux does provide a mechanism for that. An annotation would make it easier, but we don't want to introduce a annotation with broad semantics if it can't actually be applied more broadly.

For now we are going to close this since JEP 491 is going to significantly address the original concern. If anything comes up in the future, we can look to address it with a more targeted solution, and also taking into account specific constraints with blocking handling in Spring MVC.

@rstoyanchev rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2024
@rstoyanchev rstoyanchev added the for: external-project Needs a fix in external project label Nov 20, 2024
@rstoyanchev rstoyanchev removed this from the General Backlog milestone Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project in: messaging Issues in messaging modules (jms, messaging) in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants