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

Introduce cancel(boolean mayInterruptIfRunning) in ScheduledTask #28233

Closed
fcanovas1986 opened this issue Mar 25, 2022 · 0 comments
Closed

Introduce cancel(boolean mayInterruptIfRunning) in ScheduledTask #28233

fcanovas1986 opened this issue Mar 25, 2022 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@fcanovas1986
Copy link

fcanovas1986 commented Mar 25, 2022

Affects: 5.3.17


Hello everyone,

I am working on a scheduling solution and I want to cancel a scheduled task on demand. I am using ScheduledTask which has a cancel() method:

/**
* Trigger cancellation of this scheduled task.
*/
public void cancel() {
ScheduledFuture<?> future = this.future;
if (future != null) {
future.cancel(true);
}
}

If I call it, it cancels the task, and it never executes it again unless I resume the task, so, everything is good as expected. But there is a use case which is a problem for me.

I see that the call future.cancel(true); means that if the task is currently running it interrupts it immediately, without letting it finish, which could cause issues depending on the task performed. This is because the call is obligatory done with the boolean true, without any other option.

Wouldn't it be more flexible to allow cancel() to take this into consideration? It could be enough to create another method cancel(boolean mayInterruptIfRunning);.

or even removing the final accessor from this class:

public final class ScheduledTask {....}

since because of this I can't extend from it and overwrite the method mentioned. I think would be useful to have the chance to make more flexible this behavior.

Of course, please tell me if I am missing some considerations about why this class has to be final and this future.cancel(); method is called always mandatory with true.

If in the end you consider any of the changes I proposed, I would be happy to contribute.

My proposed solution:

public final class ScheduledTask {

// . . .

  /**
   * Trigger cancellation of this scheduled task with optional interruption if running.
   * 
   * @param mayInterruptIfRunning if true, it will force interruption the task if running. If false, it will let it finish before cancelling.
   */
  public void cancel(final boolean mayInterruptIfRunning) {
    ScheduledFuture<?> future = this.future;
    if (future != null) {
      future.cancel(mayInterruptIfRunning);
    }
  }

  /**
   * Trigger cancellation of this scheduled task with forced interruption if running.
   */
  public void cancel() {
    this.cancel(true);
  }

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 25, 2022
@sbrannen sbrannen changed the title Allow to call cancel method with mayInterruptIfRunning parameter to false in ScheduledTask Introduce cancel(boolean mayInterruptIfRunning) in ScheduledTask Mar 25, 2022
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Mar 25, 2022
@jhoeller jhoeller self-assigned this Mar 25, 2022
@jhoeller jhoeller removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 25, 2022
@jhoeller jhoeller added this to the 5.3.18 milestone Mar 25, 2022
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

4 participants