如何用Spring 3.0表达式语言参数化@Scheduled(fixedDelay)? [英] How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?

查看:5078
本文介绍了如何用Spring 3.0表达式语言参数化@Scheduled(fixedDelay)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Spring 3.0功能注释一个计划任务,我想设置fixedDelay作为参数从我的配置文件,而不是硬接线到我的任务类,如目前...

  @Scheduled(fixedDelay = 5000)
public void readLog(){
...
} $ b不幸的是,通过Spring表达式语言(EL)的手段,@Value返回一个String对象,它在根据fixedDelay参数的要求,无法自动转换为长整型。



感谢您的帮助。

解决方案

我猜想 @Scheduled 注释是没有问题的。所以也许一个解决方案,你会使用任务调度 XML配置。让我们考虑这个例子(复制自 Spring doc ):

 < task:scheduled-tasks scheduler =myScheduler> 
< task:scheduled ref =someObjectmethod =readLog
fixed-rate =#{YourConfigurationBean.stringValue}/>
< / task:scheduled-tasks>

...或者如果从String到Long的转换不工作,

 < task:scheduled-tasks scheduler =myScheduler> 
< task:scheduled ref =someObjectmethod =readLog
fixed-rate =#{T(java.lang.Long).valueOf(YourConfigurationBean.stringValue)}/>
< / task:scheduled-tasks>

再次,我没有尝试过任何这些设置,但我希望它可以帮助你。


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...

@Scheduled(fixedDelay=5000)
public void readLog() {
        ...
}

Unfortunately it seems that with the means of the Spring Expression Language (EL) @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.

Thanks in advance for your help on this.

解决方案

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):

<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:

<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.

这篇关于如何用Spring 3.0表达式语言参数化@Scheduled(fixedDelay)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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