Java 如何使用 Spring 3.0 表达式语言参数化 @Scheduled(fixedDelay)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2598712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?
提问by ngeek
When using the Spring 3.0 capability to annotate a scheduled task, I would like to set the fixedDelay
as parameter from my configuration file, instead of hard-wiring it into my task class, like currently...
当使用 Spring 3.0 功能注释计划任务时,我想fixedDelay
从我的配置文件中设置as 参数,而不是像当前那样将其硬连接到我的任务类中...
@Scheduled(fixedDelay = 5000)
public void readLog() {
...
}
Unfortunately it seems that with the means of the Spring Expression Language (SpEL) @Value
returns a String object which in turn is not able to be auto-boxed to a long value as required by the fixedDelay
parameter.
不幸的是,似乎使用 Spring 表达式语言 (SpEL)@Value
返回一个 String 对象,而该对象又无法根据fixedDelay
参数的要求自动装箱为长值。
采纳答案by Grzegorz Oledzki
I guess the @Scheduled
annotation is out of question. So maybe a solution for you would be to use task-scheduled
XML configuration. Let's consider this example (copied from Spring doc):
我想@Scheduled
注释是不可能的。所以也许你的解决方案是使用task-scheduled
XML 配置。让我们考虑这个例子(从Spring doc复制):
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{YourConfigurationBean.stringValue}"/>
</task:scheduled-tasks>
... or if the cast from String to Long didn't work, something like this would:
...或者如果从 String 到 Long 的强制转换不起作用,则如下所示:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="someObject" method="readLog"
fixed-rate="#{T(java.lang.Long).valueOf(YourConfigurationBean.stringValue)}"/>
</task:scheduled-tasks>
Again, I haven't tried any of these setups, but I hope it might help you a bit.
同样,我还没有尝试过任何这些设置,但我希望它可以对您有所帮助。
回答by Grzegorz Oledzki
I guess you can convert the value yourself by defining a bean. I haven't tried that, but I guess the approach similar to the following might be useful for you:
我猜你可以通过定义一个bean来自己转换值。我还没有尝试过,但我想类似于以下的方法可能对您有用:
<bean id="FixedDelayLongValue" class="java.lang.Long"
factory-method="valueOf">
<constructor-arg value="#{YourConfigurationBean.stringValue}"/>
</bean>
where:
在哪里:
<bean id="YourConfigurationBean" class="...">
<property name="stringValue" value="5000"/>
</bean>
回答by kan
You can use the @Scheduled
annotation, but together with the cron
parameter only:
您可以使用@Scheduled
注释,但cron
只能与参数一起使用:
@Scheduled(cron = "${yourConfiguration.cronExpression}")
Your 5 seconds interval could be expressed as "*/5 * * * * *"
. However as I understand you cannot provide less than 1 second precision.
您的 5 秒间隔可以表示为"*/5 * * * * *"
。但是,据我所知,您不能提供少于 1 秒的精度。
回答by Mark-A
Spring v3.2.2 has added String parameters to the original 3 long parameters to handle this. fixedDelayString
, fixedRateString
and initialDelayString
are now available too.
Spring v3.2.2 在原来的 3 个长参数中增加了 String 参数来处理这个问题。fixedDelayString
,fixedRateString
而initialDelayString
现在可以了。
@Scheduled(fixedDelayString = "${my.fixed.delay.prop}")
public void readLog() {
...
}
回答by attacomsian
In Spring Boot 2, we can use Spring Expression Language (SpPL) for @Scheduled
annotation properties:
在 Spring Boot 2 中,我们可以使用 Spring Expression Language (SpPL) 来@Scheduled
标注属性:
@Scheduled(fixedRateString = "${fixed-rate.in.milliseconds}")
public void fixedRate() {
// do something here
}
@Scheduled(fixedDelayString = "${fixed-delay.in.milliseconds}")
public void fixedDelay() {
// do something here
}
@Scheduled(cron = "${cron.expression}")
public void cronExpression() {
// do something here
}
The application.properties
file will look like this:
该application.properties
文件将如下所示:
fixed-rate.in.milliseconds=5000
fixed-delay.in.milliseconds=4000
cron.expression=0 15 5 * * FRI
That's it. Here is an articlethat explains task scheduling in detail.
就是这样。这里有一篇文章详细解释了任务调度。