簡體   English   中英

使用java spring mvc調度任務

[英]Scheduling task using java spring mvc

我需要安排一個任務在java中自動運行..我需要相同的窗口調度功能。我已經做了每天,每年但是當我來到每周調度時卡住了......沒有得到如何做到這一點。 我正在使用java calendar.Please幫助找到一個很好的解決方案。

任何幫助或想法都會很明顯

在Spring中調度任務可以通過4種方式完成,如下所示。

1.在@Scheduled注釋中使用固定延遲屬性的任務調度。

public class DemoServiceBasicUsageFixedDelay {
    @Scheduled(fixedDelay = 5000)
    // @Scheduled(fixedRate = 5000)
    public void demoServiceMethod() {
        System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
    }
}

2.在@Scheduled注釋中使用cron表達式的任務調度

@Scheduled(cron = "*/5 * * * * ?")
public void demoServiceMethod() {
    System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
}

3.使用屬性文件中的cron表達式進行任務調度。

@Scheduled(cron = "${cron.expression}")
public void demoServiceMethod() {
    System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
}

4.使用在上下文配置中配置的cron表達式的任務調度

public class DemoServiceXmlConfig {
    public void demoServiceMethod() {
        System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
    }
}

#4的XML配置

<task:scheduled-tasks>
        <task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled>
</task:scheduled-tasks>

關於http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/的更多解釋

希望這對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM