Skip to content

An option for SimpleAsyncTaskExecutor to throw an exception when limit is reached #34727

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

Closed
dsteb opened this issue Apr 7, 2025 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@dsteb
Copy link

dsteb commented Apr 7, 2025

Hello,

Use-case:

  1. We have an @Async @Retryable task that may fail (e.g. during an incident).
  2. If it fails, we retry N times for M seconds.
  3. Given N, M and RPS, there can be hundred thousands of pending @Async tasks.
  4. We would like to limit concurrency by several thousands, and reject over-limit tasks without affecting a caller of @Async method (without blocking the caller or failing the caller).
  5. We would like to use Virtual Threads.

Using default SimpleAsyncTaskExecutor with concurrencyLimit is not an option as it blocks the caller when limit is reached.

Using ThreadPoolExecutor with Virtual Threads is an option, but it is considered an anti-pattern.

Another option is not to use @Async, but just use virtual threads with a semaphore for example.

Is it possible to implement an option for SimpleAsyncTaskExecutor to throw some TaskRejectedException when limit is reached instead of blocking the caller?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 7, 2025
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 7, 2025
@jhoeller jhoeller self-assigned this Apr 7, 2025
@jhoeller jhoeller added this to the 6.2.6 milestone Apr 7, 2025
@jhoeller
Copy link
Contributor

jhoeller commented Apr 7, 2025

Good idea! I've introduced a corresponding rejectTasksWhenLimitReached flag on SimpleAsyncTaskExecutor.

This will be available in the upcoming 6.2.6 snapshot in a few minutes. Feel free to give it an early try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants