Introduce cancel(boolean mayInterruptIfRunning) in ScheduledTask #28233
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
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 acancel()
method:spring-framework/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTask.java
Lines 55 to 63 in 42a61b9
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 methodcancel(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 thisfuture.cancel();
method is called always mandatory withtrue
.If in the end you consider any of the changes I proposed, I would be happy to contribute.
My proposed solution:
Thanks
The text was updated successfully, but these errors were encountered: