use*_*587 6 spring scheduled-tasks
在我们的系统中,我们使用一个设置类来指向属性文件,这取决于它加载不同的属性文件的前夕。要访问特定属性,我们调用'Settings.getString('property_name_here')'。
在我的代码中,我将 @scheduled cron 表达式加载到一个变量并尝试传递给 @scheduled 注释,但它不起作用,
这是我的代码:在属性文件中:
cron.second=0
cron.min=1
cron.hour=14
Run Code Online (Sandbox Code Playgroud)
在构造函数中,我有:
this.cronExpression = new StringBuilder()
.append(settings.getString("cron.second"))
.append(" ")
.append(settings.getString("cron.min"))
.append(" ")
.append(settings.getString("cron.hour"))
.append(" ")
.append("*").append(" ").append("*").append(" ").append("*")
.toString();
Run Code Online (Sandbox Code Playgroud)
它创建了一个“0 1 14 * * *”的字符串,它是一个有效的 con 表达式
在计划任务中,我有:
@Scheduled(cron = "${this.cronExpression}")
public void scheduleTask() throws Exception {
....
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时它抱怨:引起:java.lang.IllegalStateException:遇到无效的@Scheduled方法'scheduleTask':Cron表达式必须包含6个字段(在“${this.cronExpression}”中找到1个)
然后我将 this.cronExpression 更改为字符串列表:
this.cronExpression = Lists.newArrayList();
this.cronExpression.add(settings.getString("cron.second"));
this.cronExpression.add(settings.getString("cron.min"));
this.cronExpression.add(settings.getString("cron.hour"));
this.cronExpression.add("*");
this.cronExpression.add("*");
this.cronExpression.add("*");
Run Code Online (Sandbox Code Playgroud)
但仍然出现相同的错误,那么 cron 表达式到底应该是什么?
小智 8
我做了什么,它对我有用:
在属性文件中:
my.cron.expression=0 3 * * * ?
Run Code Online (Sandbox Code Playgroud)
在代码中:
@Scheduled(cron = "${my.cron.expression}")
public void scheduleTask(){
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9563 次 |
| 最近记录: |