Lets say I have some rest api where arguments are time when to execute method and second argument is name of the method in class. What is the best way for invoking call of this method in certain time (just once) in spring-boot application ?
Asked
Active
Viewed 278 times
1 Answers
0
First, enable scheduling in your spring-boot application:
@SpringBootApplication
@EnableScheduling
public class Application {
// ...
Then, inject the TaskScheduler
bean and schedule the task programmatically every time the user invokes the REST method:
public class MyScheduler {
@Autowired
private TaskScheduler scheduler;
public void scheduleNewCall(Date dateTime) {
scheduler.schedule(this::scheduledMethod, dateTime);
}
public void scheduledMethod() {
// method that you wish to run
}
}
However, you should also think about limiting the amount of calls to this method, otherwise the malicious user could schedule a lot of tasks and overflow the task pool.

Forketyfork
- 7,416
- 1
- 26
- 33
-
nice thx for the example. I was curious if there is something better then just taskScheduler. And yea there is also some authentication so just few admins can plan task – hudi Feb 27 '19 at 09:05