如何使用Spring 3.0表达式语言参数化@Scheduled(fixedDelay)?

发布于 2024-08-28 02:23:27 字数 320 浏览 7 评论 0原文

当使用 Spring 3.0 功能注释计划任务时,我想将 fixedDelay 设置为我的配置文件中的参数,而不是像当前那样将其硬连接到我的任务类中......

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

不幸的是似乎通过 Spring 表达式语言 (SpEL) @Value 返回一个 String 对象,而该对象又无法按照 fixedDelay 的要求自动装箱为 long 值 参数。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

战皆罪 2024-09-04 02:23:27

Spring v3.2.2在原来的3个长参数的基础上增加了String参数来处理这个问题。 fixedDelayStringfixedRateStringinitialDelayString 现在也可用。

@Scheduled(fixedDelayString = "${my.fixed.delay.prop}")
public void readLog() {
        ...
}

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.

@Scheduled(fixedDelayString = "${my.fixed.delay.prop}")
public void readLog() {
        ...
}
胡渣熟男 2024-09-04 02:23:27

您可以使用 @Scheduled 注释,但只能与 cron 参数一起使用:

@Scheduled(cron = "${yourConfiguration.cronExpression}")

您的 5 秒间隔可以表示为 "*/5 * * * * * “。但是据我了解,您无法提供低于 1 秒的精度。

You can use the @Scheduled annotation, but together with the cron parameter only:

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

浅暮の光 2024-09-04 02:23:27

我猜 @Scheduled 注释是没有问题的。因此,也许您的解决方案是使用任务计划 XML 配置。让我们考虑这个例子(复制自 Spring doc ):

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

...或者如果从 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>

同样,我还没有尝试过任何这些设置,但我希望它可以对您有所帮助。

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.

乜一 2024-09-04 02:23:27

在 Spring Boot 2 中,我们可以使用 Spring 表达式语言 (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
}

application.properties 文件将如下所示:

fixed-rate.in.milliseconds=5000
fixed-delay.in.milliseconds=4000
cron.expression=0 15 5 * * FRI

就是这样。这是一篇文章,详细解释了任务调度。

In Spring Boot 2, we can use Spring Expression Language (SpPL) for @Scheduled annotation properties:

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

fixed-rate.in.milliseconds=5000
fixed-delay.in.milliseconds=4000
cron.expression=0 15 5 * * FRI

That's it. Here is an article that explains task scheduling in detail.

永不分离 2024-09-04 02:23:27

我想你可以通过定义一个bean来自己转换该值。 我还没有尝试过,但我想类似于以下的方法可能对您有用:

<bean id="FixedDelayLongValue" class="java.lang.Long"
      factory-method="valueOf">
    <constructor-arg value="#{YourConfigurationBean.stringValue}"/>
</bean>

其中:

<bean id="YourConfigurationBean" class="...">
         <property name="stringValue" value="5000"/>
</bean>

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 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>
流年里的时光 2024-09-04 02:23:27

如果延迟配置为持续时间,例如 5s,则 问题:支持占位符中灵活的持续时间解析

@Scheduled(fixedRateString = 
   "#{T(org.springframework.boot.convert.DurationStyle).detectAndParse('${app.update-cache}')}")

我不明白,为什么问题已关闭并且持续时间尚未实现
顺便说一句:我已经尝试过,但没有成功,更漂亮一点:

@Scheduled(fixedRateString = "'PT'+'${app.update-cache}'") and
@Scheduled(fixedRateString = "'PT'.concat('${app.update-cache}')

In case of delay configured as Duration, e.g. 5s, there are not pretty, but working solution from issues: support flexible duration parsing in placeholders

@Scheduled(fixedRateString = 
   "#{T(org.springframework.boot.convert.DurationStyle).detectAndParse('${app.update-cache}')}")

I do not understand, why issue is closed and Duration is not realized yet
BTW: I've tried, but without success, a little more pretty:

@Scheduled(fixedRateString = "'PT'+'${app.update-cache}'") and
@Scheduled(fixedRateString = "'PT'.concat('${app.update-cache}')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文