如何在Spring config.xml中配置Cron时区? [英] How to configure Cron timezone in a Spring config.xml?

查看:133
本文介绍了如何在Spring config.xml中配置Cron时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Cron任务的Spring配置xml文件。该任务会在我的计算机上定期执行。
如何在xml文件中配置此任务以使用莫斯科时区(与我的时区不同?)?

I have a Spring configuration xml file with a Cron task. The task gets executed periodically on my machine. How can I configure this task in the xml file to use Moscow timezone (that is different from mine)?

<task:scheduler id="scheduler" pool-size="1"/>

<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="productTask" method="loadProduct" cron="0 0 10 * * *"/>
</task:scheduled-tasks>

编辑:我仔细检查了语法并稍稍更改了代码一点。但这对我仍然无效。
下面提供了我想到的最后一个配置。在这里,我得到以下异常:构造函数引发了异常;嵌套异常是java.lang.IllegalArgumentException:Cron表达式必须包含6个字段(在 moscowTimeCronSchedule中找到1个字段)

I have double checked the syntax and changed the code a little bit. But it still doesn't work for me. Below I've provided the last configuration I came up with. Here I'm getting the following exception: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Cron expression must consist of 6 fields (found 1 in "moscowTimeCronSchedule")

因此任务调度程序需要cron表达式,而不是 cron = moscowTimeCronSchedule 。我需要弄清楚如何传递一个bean引用而不是纯cron表达式。

So task "scheduler" requires cron expression instead of cron="moscowTimeCronSchedule". I need to figure out how is it possible to pass a bean reference to it instead of the pure cron expression.

<task:scheduler id="scheduler" pool-size="1"/>

    <bean id="moscowTimeZone" class="java.util.TimeZone" factory-method="getTimeZone">
        <constructor-arg type="java.lang.String" value="Europe/Moscow"/>
    </bean>

    <bean id="moscowTimeCronSchedule" class="org.springframework.scheduling.support.CronTrigger"
          c:expression="*/15 * * * * *"
          c:timeZone-ref="moscowTimeZone"/>

    <task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="productTask" method="loadProduct" cron="moscowTimeCronSchedule"/>
    </task:scheduled-tasks>

我发现此链接很有帮助,但未回答以下问题:如何通过
http://websystique.com/spring/spring-job-scheduling-using-xml-configuration/

推荐答案

Spring配置CronTask时,它使用简单的构造函数形式,该形式接受 String 。您需要使用第二个构造函数,该构造函数接受 CronTrigger 。这应该可以工作(尽管,当然,我还没有测试过):

When Spring configures a CronTask, it uses a simple constructor form which accepts String. What you require is for it to use the second constructor, which accepts a CronTrigger. This should work (although, admittedly, I haven't tested it):

<bean
  id="moscowTimeZone"
  class="java.util.TimeZone"
  factory-method="getTimeZone">
    <constructor-arg type="java.lang.String" value="Europe/Moscow"/>
</bean>

<bean
  id="moscowTimeCronSchedule"
  class="org.springframework.scheduling.support.CronTrigger">
    <constructor-arg type="java.lang.String" value="0 0 10 * * *"/>
    <constructor-arg type="java.util.TimeZone" ref="moscowTimeZone"/>
</bean>

<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="productTask" method="loadProduct" trigger="moscowTimeCronSchedule"/>
</task:scheduled-tasks>

我在这里做什么:


  1. 构造了对莫斯科时区的引用,并在春季配置中将其保存为bean。

  1. Constructed a reference to moscow time zone and saved it as a bean in spring configuration.

构造了一个Cron Trigger实例另一个使用该时区和cron表达式的bean

Constructed a Cron Trigger instance as another bean using that time zone and a cron expression

在计划的任务构造函数中使用了Cron触发器。

Used the Cron Trigger in scheduled task constructor.

诚然,此解决方案有点冗长,但听起来应该做得到。

Admittedly, this solution is a bit long-winded, but it sounds like it should do its job.

这篇关于如何在Spring config.xml中配置Cron时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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