网站备案都审核什么资料,wordpress媒体库显示空白,域客式单页网站能申请域名吗,营销型网站建设沈阳简介
Spring 内部有一个 task 是 Spring 自带的一个设定时间自动任务调度#xff0c;提供了两种方式进行配置#xff0c;一种是注解的方式#xff0c;而另外一种就是 XML 配置方式了;注解方式比较简洁#xff0c;XML 配置方式相对而言有些繁琐#xff0c;但是应用场景的不…简介
Spring 内部有一个 task 是 Spring 自带的一个设定时间自动任务调度提供了两种方式进行配置一种是注解的方式而另外一种就是 XML 配置方式了;注解方式比较简洁XML 配置方式相对而言有些繁琐但是应用场景的不同两者又各有优点所以具体使用还是根据需求来划分因为任务调度这样的需求通常改动都是比较多的如果用注解的方式改动就变得麻烦了必须去重新编译所以更多的时候推荐使用XML配置的方式
使用 XML 配置
定义一个 Task 类添加一个测试方法
public class Task {public void cron() {System.out.println(new Date() , 定时任务执行中 ...);}
}在 spring.xml 配置文件中添加如下配置
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:taskhttp://www.springframework.org/schema/taskxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd!-- 定时任务 --bean idtask classcom.review.spring.model.Task/!-- 创建任务池 -- task:scheduler idtaskScheduler pool-size100 /!-- 定义定时任务 -- task:scheduled-tasks schedulertaskScheduler!-- 表示周一到周五的每分钟的第五秒执行--task:scheduled reftask methodcron cron5 * * * * MON-FRI//task:scheduled-tasks
/beans在测试类中初始化容器定时任务就会自动执行
public class TestTask {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(classpath:/spring.xml);}
}使用注解配置
定义 Task 类添加如下代码
Component
public class Task {Scheduled(cron0/2 * * * * *) // 每隔 2 秒运行一次public void cron() {System.out.println(new Date() , 定时任务执行中 ...);}
}在 spring.xml 配置文件中添加如下配置
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:taskhttp://www.springframework.org/schema/taskxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdcontext:component-scan base-packagecom.review.spring/task:annotation-driven/ !-- 如果是代码式配置, 需要在配置类添加注解 EnableScheduling --
/beanscronExpression 的配置说明 cron 表达式是一个字符串字符串以 5 或 6 个空格隔开分为 6 或 7 个域每一个域代表一个含义cron 有如下两种语法格式
(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2) Seconds Minutes Hours DayofMonth Month DayofWeek(常用) 注意Spring 中的任务调度以及其 ThreadPoolTaskScheduler 中的 cron 表达式是不支持 L W 字符的所以最后一天和最近的工作日就无法定义在 cron 表达式中
扩展如何实现每月最后一天执行任务 方式一每个月的最后一天因为不同的月份最后一天是不一样的但是好在有个范围28 – 31那么我们可以结合 cron 表达式 代码实现控制每月的最后一天执行任务代码如下
Scheduled(cron 0 0 0 28-31 * ?)
public void execute() {final Calendar c Calendar.getInstance();// 判断是不是最后一天if (c.get(Calendar.DATE) c.getActualMaximum(Calendar.DATE)) {// 你的业务逻辑代码}
}方式二在 cron 表达式中L 表示最后一个的意思所以可定义为
Scheduled(cron * * * L * ?)前提是定义的 cron 表达式支持 L W 指定例如Quartz 等框架如果是 Spring 自带定时器则不支持