记录下Spring
自带的定时任务用法。html
首先配置spring开启定时任务java
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<task:annotation-driven /> <!-- 定时器开关-->
<bean id="myTask" class="com.spring.task.MyTask"></bean>
<task:scheduled-tasks>
<!-- 这里表示的是每隔五秒执行一次 -->
<task:scheduled ref="myTask" method="show" cron="*/5 * * * * ?" />
<task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/>
</task:scheduled-tasks>
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.spring.task" />
</beans>
复制代码
定义本身的任务执行逻辑spring
package com.spring.task;
/**
* 定义任务
*/
public class MyTask {
public void show() {
System.out.println("show method 1");
}
public void print() {
System.out.println("print method 1");
}
}
复制代码
package com.spring.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 基于注解的定时器
*/
@Component
public class MyTask2 {
/**
* 定时计算。天天凌晨 01:00 执行一次
*/
@Scheduled(cron = "0 0 1 * * *")
public void show() {
System.out.println("show method 2");
}
/**
* 启动时执行一次,以后每隔2秒执行一次
*/
@Scheduled(fixedRate = 1000*2)
public void print() {
System.out.println("print method 2");
}
}
复制代码
这样,当项目启动,定时任务就会按照规则按时执行了。springboot
Spring Boot中使用更加方便。bash
springboot starter
包<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
复制代码
@EnableScheduling
,开启定时任务功能@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
复制代码
@Component
public class MyTask3 {
private int count=0;
@Scheduled(cron="*/6 * * * * ?")
private void process() {
System.out.println("this is scheduler task runing "+(count++));
}
}
复制代码
先来看看@Scheduled
注解的源码服务器
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String cron() default "";
String zone() default "";
long fixedDelay() default -1;
String fixedDelayString() default "";
long fixedRate() default -1;
String fixedRateString() default "";
long initialDelay() default -1;
String initialDelayString() default "";
}
复制代码
能够看出,注解中能够传8种参数:spring-boot
java.util.TimeZone
中的zoneIdString
类型String
类型String
类型Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每个域表明一个含义,Cron有以下两种语法格式:this
每个域可出现的字符以下:spa
每个域都使用数字,但还能够出现以下特殊字符,它们的含义是:code
*
:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。?
:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。由于DayofMonth和 DayofWeek会相互影响。例如想在每个月的20日触发调度,无论20日究竟是星期几,则只能使用以下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,若是使用*表示无论星期几都会触发,实际上并非这样。-
:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次。/
:表示起始时间开始触发,而后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次。,
:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。L
:表示最后,只能出如今DayofWeek和DayofMonth域,若是在DayofWeek域使用5L,意味着在最后的一个星期四触发。W
:表示有效工做日(周一到周五),只能出如今DayofMonth域,系统将在离指定日期的最近的有效工做日触发事件。例如:在 DayofMonth使用5W,若是5日是星期六,则将在最近的工做日:星期五,即4日触发。若是5日是星期天,则在6日(周一)触发;若是5日在星期一 到星期五中的一天,则就在5日触发。另一点,W的最近寻找不会跨过月份。LW
:这两个字符能够连用,表示在某个月最后一个工做日,即最后一个星期五。#
:用于肯定每月第几个星期几,只能出如今DayofMonth域。例如在4#2,表示某月的第二个星期三。参考 cron表达式例子