Spring Cron 调度程序“禁用模式" [英] Spring Cron scheduler “disable pattern”

查看:34
本文介绍了Spring Cron 调度程序“禁用模式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序从属性文件加载一些 cron 模式.我正在使用这样的 @Scheduled 注释:

My application loads some cron patterns from a properties file. I'm using the @Scheduled annotation like this:

@Scheduled(cron = "${config.cronExpression:0 0 11,23 * * *}")

现在我想禁用一些任务,最简单的解决方案是输入一个永远不会运行的 cron 模式.为了做到这一点,我考虑使用仅在过去特定日期执行的 cron 表达式.但不幸的是,Spring cron 表达式不允许在过去添加年份或日期.

Now I want to disable some tasks and the easiest solution would be to enter a cron pattern which will never run. In order to do this, I thought about using a cron expression that only executes at a specific day in the past. But unfortunately the Spring cron expressions don't allow to add a year or a date in the past.

有没有永远不会运行的模式?

Is there any pattern that will never run?

推荐答案

从 Spring 5.1.0 开始,@Scheduled 注释可以接受 "-" 作为cron 表达式来禁用 cron 触发器.

As of Spring 5.1.0 the @Scheduled annotation can accept "-" as the cron expression to disable the cron trigger.

根据 Javadocs:

特殊值 "-" 表示禁用的 cron 触发器,主要用于由 ${...} 占位符解析的外部指定值.

The special value "-" indicates a disabled cron trigger, primarily meant for externally specified values resolved by a ${...} placeholder.

这篇关于Spring Cron 调度程序“禁用模式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆