gpt4 book ai didi

java - 使用属性文件禁用调度

转载 作者:行者123 更新时间:2023-11-30 08:54:59 24 4
gpt4 key购买 nike

我想通过外部配置文件禁用 spring 调度。我有配置文件设置,下面示例中的任务记录了以下内容。INFO MainTaskScheduler:36 - 计划任务:更新转换后的预订为假。 所以大部分时间我都在那里。

我希望实现的是不必在每个任务方法中放置逻辑来确定是否已启用调度属性。

所以在我的配置文件中有这样的东西(这不是有效的代码)@EnableScheduling(${enable.scheduling})

我的工作片段


应用配置

@Configuration
@EnableTransactionManagement
@EnableScheduling
@ComponentScan( /*etc*/})
public class AppConfiguration {

}

主任务调度器

@Component
public class MainTaskScheduler {

private Logger log = LoggerFactory.getLogger(getClass());

@Value("${enable.scheduling}")
private Boolean enableScheduling;

@Scheduled(fixedRate=300) // every 5 minutes -- check if any existing quotes have been converted to bookings
public void updateConvertedBookings() {
log.info("scheduled task: Update converted bookings "+enableScheduling);
// logic for class here
}
}

application.properties

enable.scheduling=false

最佳答案

如果您使用的是 spring boot,则可以使用 @ConditionalOnExpression 注解来启用或禁用调度:

@ConditionalOnExpression("'${enable.scheduling}'=='true'")

关于java - 使用属性文件禁用调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29254932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com