Issue
I have two different tasks, let's call them A and B.
Task A should start immediately (t0) and stop after a fixed time (t1), task B should start after a fixed time (t1) and run until i stop the service.
Both task A and B should do something every x seconds (for convenience, print a string). I know that in Springboot i can achive that using this annotation:
@Scheduled(fixedDelay = 6000)
private void taskA(){
print("A")
}
But i have no clue how to start and stop each tasks after the time window has passed. I have made a simple scheme to help you understand better.
Thanks
Solution
You can schedule a task programatically via org.springframework.scheduling.TaskScheduler
.
e.g.
@Autowired
private TaskScheduler taskScheduler;
void scheduleTask(){
final int xSeconds = 2000;
PeriodicTrigger periodicTrigger = new PeriodicTrigger(xSeconds, TimeUnit.SECONDS);
taskScheduler.schedule(
() -> System.out.println("task B"),
periodicTrigger
);
}
This acticle can also be helpful.
Answered By - Petr Aleksandrov
Answer Checked By - Terry (JavaFixing Volunteer)