我需要安排一个任务在 java 中自动运行。我需要与窗口调度相同的功能。我已经完成了每日、每年的任务,但当我进行每周调度时卡住了。不知道如何执行此操作。我正在使用 java 日历。请帮助找到一个好的解决方案。
任何帮助或想法将不胜感激
最佳答案
在 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
注解
@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/ 的更多解释
希望对你有帮助。
关于java - 使用java spring mvc调度任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26425889/