[英]Springboot: start a and stop programmatically a scheduled task
I have two different tasks, let's call them A and B.我有两个不同的任务,我们称它们为 A 和 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.任务 A 应立即启动 (t0) 并在固定时间 (t1) 后停止,任务 B 应在固定时间 (t1) 后启动并运行直到我停止服务。
Both task A and B should do something every x seconds (for convenience, print a string).任务 A 和 B 都应该每 x 秒执行一次(为方便起见,打印一个字符串)。 I know that in Springboot i can achive that using this annotation:
我知道在 Springboot 中我可以使用这个注释来实现:
@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.但是我不知道在 window 过去之后如何启动和停止每个任务。 I have made a simple scheme to help you understand better.
我做了一个简单的方案来帮助你更好地理解。
Thanks谢谢
You can schedule a task programatically via org.springframework.scheduling.TaskScheduler
.您可以通过
org.springframework.scheduling.TaskScheduler
以编程方式安排任务。
eg例如
@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.这篇文章也很有帮助。
You can combine fixedRate
with initialDelay
annotation.您可以将
fixedRate
与initialDelay
注释结合使用。 So taskB runs after an initial delay of 6100 ms.因此 taskB 在 6100 毫秒的初始延迟后运行。
A --------- B ----- A -------- B ----- A ------> A --------- B ----- A -------- B ----- A ------>
t(0) t(5900) t(6000) t(1900) t(12000) t(0) t(5900) t(6000) t(1900) t(12000)
@Scheduled(fixedRate = 6000)
private void taskA(){
System.out.println("A");
}
@Scheduled(fixedRate = 6000, initialDelay = 5000)
private void taskB(){
System.out.println("B");
}
The @Scheduled
annotation is for stuff that runs forever. @Scheduled
注释适用于永远运行的东西。 Consider TaskScheduler
for the task that must stop and @Scheduled
for the task that should run until you stop the service.将
TaskScheduler
用于必须停止的任务,将@Scheduled
用于在您停止服务之前应该运行的任务。
Here is some code:这是一些代码:
@Component
@RequiredArgsConstructor
public class Blam
implements
Runnable
{
private int count = 0;
private ScheduledFuture<?> scheduledFuture = null;
private final TaskScheduler taskScheduler;
@Override
public void run()
{
if (count < 6)
{
System.out.printf("%d: blam.taskA\n", count);
++count;
}
else
{
scheduledFuture.cancel(true);
}
}
@PostConstruct
public void postConstruct()
{
scheduledFuture = taskScheduler.scheduleWithFixedDelay(this, 1000);
}
}
@Component
public class Kapow
{
@Scheduled(initialDelay = 6000, fixedDelay = 1000)
public void taskB()
{
System.out.println("Kapow.taskB");
}
}
@RequiredArgsConstructor
is a Lombok annotation, I highly recommend using Lombok. @RequiredArgsConstructor
是一个 Lombok 注释,我强烈推荐使用 Lombok。 If you don't use Lombok, just inject the TashScheduler
however you choose.如果您不使用 Lombok,只需根据您的选择注入
TashScheduler
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.