2014-10-17 61 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/

希望这可以帮助你。