2017-11-24 11 views
0

私はCronタスクを持つSpring設定XMLファイルを持っています。タスクは私のマシン上で定期的に実行されます。 モスクワのタイムゾーン(これは私とは違う)を使用するには、xmlファイルでこのタスクをどのように設定できますか?Spring config.xmlでCronタイムゾーンを設定するには?

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

編集:私は二重の構文をチェックし、コードを少し変更しました。しかしそれはまだ私のために働かない。 以下は、私が思いついた最後の設定を示しました。ここでは、次の例外が発生します。Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Cron expression must consist of 6 fields (found 1 in "moscowTimeCronSchedule")

タスク「スケジューラ」タスクでは、cron="moscowTimeCronSchedule"ではなくcron式が必要です。私は純粋なcron式ではなく、bean参照を渡すことが可能であることを理解する必要があります。

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

私は、このリンクが役に立ったと評価していますが、質問に答えていない:どのように代わりにcronの発現のBeanを渡します。 http://websystique.com/spring/spring-job-scheduling-using-xml-configuration/

+0

"30分ごとに実行する"があります。マシンが何とか '3:15'のようなゾーンオフセットになっていないかぎり、このcronはタイムゾーンの恩恵を受けません。 –

+0

@ M.Prokhorovうわー..すみません。実際の設定には 'cron =" 0 0 10 * * * "'正しい投稿が編集されます。 – samba

答えて

1

SpringがCronTaskを設定するとき、それはStringを受け入れる単純なコンストラクタフォームを使用します。あなたが必要とするのは、それがCronTriggerを受け入れる2番目のコンストラクタを使用することです。 (確かに、私はそれをテストしていない、が)これは動作するはずです:

  1. は、モスクワのタイムゾーンへの参照を構築し、春にBeanとしてそれを保存した:私はここにいた何

    <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.lang.TimeZone" ref="moscowTimeZone"/> 
    </bean> 
    
    <task:scheduled-tasks scheduler="scheduler"> 
        <task:scheduled ref="productTask" method="loadProduct" trigger="moscowTimeCronSchedule"/> 
    </task:scheduled-tasks> 
    

    構成。その時間帯を使用して、別のBeanとcron発現

  2. としてクーロントリガインスタンスを構築し

  3. は、スケジュールされたタスクのコンストラクタでクーロントリガーを使用しました。

確かに、この解決策は少し長めですが、その仕事をしなければならないように聞こえます。

+0

ありがとうございます。しかし、私は 'org.springframework.beans.factory.xml.XmlBeanDefinitionStoreExceptionを取得しています:型 'のコンテンツ型が要素のみであるため、' constructor-arg '要素は文字[子]を持つことができません。'何かが ' Europe/Moscow ' – samba

+0

' 'を試してください。 –

+0

@samba、それは助けに/終わりに仕事をしましたか? –

関連する問題