2

我每天都在尝试执行一个方法,我已经使用 Spring 添加了调度程序,但它没有被执行。

<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="logDeletionTask" method="deleteExpiredLogs" cron="0 0 0 * * ?" />
</task:scheduled-tasks>
<task:scheduler pool-size="25" id="myScheduler"/>
4

1 回答 1

1

对我来说,您正在寻找的 cron 表达式是:0 0 12 * * ?

这是一个适合您的工作示例:

应用上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <bean id="logDeletionTask" class="task.Task" />

    <task:scheduled-tasks scheduler="myScheduler">
        <task:scheduled ref="logDeletionTask" method="deleteExpiredLogs" cron="0 0 12 * * ?" />
    </task:scheduled-tasks>

    <task:scheduler pool-size="25" id="myScheduler"/>
</beans>

任务豆:

package task;

import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Task {

    public static void main(String[] args) throws InterruptedException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        while (true) {
            Thread.sleep(1000);
        }
    }

    public void deleteExpiredLogs() {
        System.out.println(new Date());
    }
}
于 2015-10-01T12:15:44.200 回答