如何在不重新启动 tomcat 服务器的情况下从属性文件更改 spring 调度程序的 cron 表达式?

How to change cron expression of spring scheduler from properties file without restarting tomcat server?

我正在使用 spring scheduler.This 工作正常但是当我每次需要重新启动 tomcat [=22] 时更改 application.properties 中的 cron.expression 值=] 有什么方法可以使它动态化,就像自动更改会反映出来一样?我也做了 google 但没有为我的 app.I 提供任何解决方案,给出的代码片段如下:

application.properties

cron.expression=0 58 23 * * ?

@Scheduled(cron = "${cron.expression}", zone = "IST")
public void sendEmail() throws Exception {



}

注释参数不是动态的。因此,如果您想更改表达式,则必须重新启动。这是对您问题的简短回答。

这是link that explains about Spring provided @EnableScheduling which is very interesting but still does not overcome the static nature of the parameter and hence the need for restart

BTW I wrote an Open Source library called MgntUtils that provides some infrastructure for creating scheduled tasks. It takes more effort to create a running task with that framework then just use @Scheduledannotation, but it gives you more humanly readable scheduling properties such as "5h", or "20m" for each 5 hours or 20 minutes respectively. (You can read about the solution here).
However, this solution is still not dynamic and will require restart upon parameter value change. But since it is Open source you can get the code and modify it for your needs and to make it dynamic i.e. reacting to parameter change without restart. You can get the library at Maven Central or GitHub

您不能从注释动态更新,但是 CronTrigger

在其构造函数中采用 cron 表达式,因此您可以直接从检查 属性 更新的任何代码中使用它。

如果您在 spring-cloud 上工作,这是一项简单的任务。 Spring 有一个名为 'spring cloud config' 的项目,它可以帮助您实现属性的外部化,并且只要有更改,该更改就会推送到您的服务。看看setting up spring cloud config

您可能会找到很多相同的示例。如果您正在寻找代码,请告诉我。