spring定时任务的用法

来源:互联网 发布:java 国际化 编辑:程序博客网 时间:2024/05/19 20:01

Spring Task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种.

XML配置文件方式

编写作业类

就是即普通的Java类,如下,
* 定时任务1

import java.util.Date;public class TaskJob {    public void myMethod() {        System.out.println(new Date().toLocaleString() + " 我是后台任务1...");    }} 
  • 定时任务2
定时任务2import java.util.Date;public class TaskJob2 {    public void myMethod() {        System.out.println(new Date().toLocaleString() + " 我是后台任务2...");    }}

在spring配置文件头中添加命名空间及描述

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

spring配置文件中设置具体的任务

<!-- 定时任务要执行的方法 --><bean id="taskJob" class="com.task.TaskJob" /><bean id="taskJob2" class="com.task.TaskJob2" /><!--用于定时任务的执行 --><task:scheduled-tasks>    <!-- 指定要运行的类的方法,每2秒执行一次 -->    <task:scheduled ref="taskJob" method="myMethod" cron=" 0/2 * * * * ?" />    <!-- 指定要运行的类的方法,每2秒执行一次 -->    <task:scheduled ref="taskJob2" method="myMethod" cron=" 0/3 * * * * ?" /></task:scheduled-tasks>

注解的方式

注解@Scheduled的定义

@Target({java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.ANNOTATION_TYPE})  @Retention(RetentionPolicy.RUNTIME)  @Documented  public @interface Scheduled  {    public abstract String cron();    public abstract long fixedDelay();    public abstract long fixedRate();  } 

可以看出该注解有三个方法或者叫参数,分别表示的意思是:
* cron:指定cron表达式
* fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
* fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

编写pojo

import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Component("taskJob")public class TaskJob {    @Scheduled(cron = " 0/2 * * * * ?") // 每两秒执行一次    public void myMethod() {        System.out.println(new Date().toLocaleString() + " 我是基于注解的后台任务...");    }}

添加task相关的配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="          http://www.springframework.org/schema/task         http://www.springframework.org/schema/task/spring-task-3.0.xsd        http://www.springframework.org/schema/beans                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:annotation-config />    <!-- spring扫描注解的配置 -->    <context:component-scan base-package="com.antation" />    <!-- 开启这个配置,spring才能识别@Scheduled注解 -->    <task:annotation-driven scheduler="qbScheduler" mode="proxy" />    <task:scheduler id="qbScheduler" pool-size="10" /></beans>

说明:理论上只需要加上这句配置就可以了,这些参数都不是必须的。

测试代码

  • Java程序
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AnnationTaskTest {    public static void main(String[] args) {        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-task.xml");        try {            Thread.sleep(Integer.MAX_VALUE);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}
  • 运行结果
2017-6-18 20:19:40 我是基于注解的后台任务...2017-6-18 20:19:42 我是基于注解的后台任务...2017-6-18 20:19:44 我是基于注解的后台任务...

一些常见的CRON表达式

  • “0 0 12 * * ?” 每天中午十二点触发
  • “0 15 10 ? * *” 每天早上10:15触发
  • “0 15 10 * * ?” 每天早上10:15触发
  • “0 15 10 * * ? *” 每天早上10:15触发
  • “0 15 10 * * ? 2005” 2005年的每天早上10:15触发
  • “0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发
  • “0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
  • “0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
  • “0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
  • “0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
  • “0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发
原创粉丝点击