使用java spring mvc调度任务

时间:2014-10-17 13:15:50

标签: java calendar quartz-scheduler

我需要安排一个任务在java中自动运行..我需要相同的窗口调度功能。我已经做了每天,每年但是当我来到每周调度时卡住了...没有得到如何做到这一点。我正在使用java日历。请帮助找到一个好的解决方案。

任何帮助或想法都会很明显

1 个答案:

答案 0 :(得分:7)

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

的更多解释

希望这会对你有所帮助。

相关问题