【发布时间】:2025-12-11 21:00:01
【问题描述】:
我已经安排了在一定时间后不断运行的作业。
现在我想仅在满足条件时运行此计划作业。条件是在运行时获得的,它不依赖于任何配置参数。
我如何做到这一点。我知道 Spring Boot 4.x 提供了这个名为 Condition 的接口。但不知何故,我的代码不起作用。
这是我的代码...
计划任务
@Configuration
public class ScheduleTask {
@Scheduled(fixedRateString = "5000")
@Conditional(SchedulerCondition.class)
public void pollDepots() {
System.out.println("Running");
}
}
条件类
public class SchedulerCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return false;
//Here some condition needs to be implemented which is not dependent on the parameters of this method.
}
}
等待您的回复。 祝你有美好的一天。
【问题讨论】:
标签: spring-boot conditional scheduled-tasks