[英]Scheduling task using java spring mvc
I need to schedule a task to run automatically in java..I need the same functionality of window scheduling.I have done for daily,yearly but stuck when i came to weekly scheduling..not getting how to do this. 我需要安排一个任务在java中自动运行..我需要相同的窗口调度功能。我已经做了每天,每年但是当我来到每周调度时卡住了......没有得到如何做到这一点。 I am using java calendar.Please help to find one good solutions. 我正在使用java calendar.Please帮助找到一个很好的解决方案。
Any help or ideas would be appreciable 任何帮助或想法都会很明显
Scheduling a task in Spring can be done in 4 ways, as shown below. 在Spring中调度任务可以通过4种方式完成,如下所示。
1. Task scheduling using fixed delay attribute in @Scheduled
annotation. 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. Task scheduling using cron expression in @Scheduled
annotation 2.在@Scheduled
注释中使用cron表达式的任务调度
@Scheduled(cron = "*/5 * * * * ?")
public void demoServiceMethod() {
System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
}
3. Task scheduling using cron expression from properties file. 3.使用属性文件中的cron表达式进行任务调度。
@Scheduled(cron = "${cron.expression}")
public void demoServiceMethod() {
System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
}
4. Task scheduling using cron expression configured in context configuration 4.使用在上下文配置中配置的cron表达式的任务调度
public class DemoServiceXmlConfig {
public void demoServiceMethod() {
System.out.println("Method executed at every 5 seconds. Current time is :: " + new Date());
}
}
XML config for #4 #4的XML配置
<task:scheduled-tasks>
<task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled>
</task:scheduled-tasks>
More explanation on http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/ 关于http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/的更多解释
Hope this helps you. 希望这对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.