2014-10-17 110 views
4

我需要安排一個任務在java中自動運行..我需要與窗口調度功能相同的功能。我已經完成了每日,每年,但當我來到每週調度時卡住..沒有得到這個怎麼做。我正在使用Java日曆。請幫助找到一個好的解決方案。使用java彈簧調度任務mvc

任何幫助或想法將明顯

+0

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#scheduling-quartz值得一讀。 – 2014-10-17 13:17:57

+0

結帳[任務執行和計劃](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html)一章 – ponomandr 2014-10-17 13:18:36

回答

6

安排在春天有個任務可以在4種方式來完成,如下圖所示。

1.使用@Scheduled註釋中的固定延遲屬性進行任務調度。使用cron表達式使用cron表達式從屬性文件

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

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

3.任務調度。

@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()); 
    } 
} 

XML配置爲#4

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

希望這可以幫助你。