根据数据库值在Spring Boot中启用或禁用计划的作业

问题描述

我需要使用@Scheduled批注在Spring Boot应用程序中使用cron样式的模式安排作业。

在将cron模式存储在配置属性文件中之前,我已经使用过cron作业。但是在这种情况下,当数据库中维护的标志变为true时,我想禁用cron作业。这样的事情我想实现。

或者在达到所需状态时,还有其他方法可以禁用数据库轮询吗?

@Service
@ConditionalOnProperty("yourConditionDBPropery")
public class SchedulingService {

//Run this task when flag in DB is set to true
@Scheduled
public void task1() {...}

@Scheduled
public void task2() {...}

}

解决方法

根据Baeldung的说法,conditional property来自配置,而不是数据库。

我建议这样:

@Service
@ConditionalOnProperty("yourConditionDBPropery")
public class SchedulingService {

//Run this task when flag in DB is set to true
@Scheduled
public void task1() {
    if(flag) {
        ....
    }
}

@Scheduled
public void task2() {...}

}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...