Spring Task

1、定时任务

 

 业务场景:

  1) 每天凌晨备份数据

  2) 每个月清空一下日志

  3) 页面上投放的广告,每个月过期

  4) 每三个月清空一下cookie

 

 

  定时任务:某个功能、业务,需要在规定的时间内,频度、间隔运行

 

2、三种 

  1) java自带的API:

    java.util.Timer类  定时器类

    java.util.TimerTask类  定时任务类

   

    可以实现指定频度间隔运行、不能在指定时间运行

  2) Quartz框架 开源 功能强大 使用起来稍显复杂

  3) Spirng3.0 以后自带的task调度工具,比Quartz更加简单方便

 

二、开发环境准备

   

    1) 创建一个javaweb工程 (maven工程)

    2) 引入相关就jar包

      pom: spring-context  spring-web

    3) 基本的配置

      web.xml 中配置Spring监听器

<web-app>
  <!-- Spring配置文件位置 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- Spring核心监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

接口和实现类:

public interface TaskService {

    void firstTask();

    void secondTask();

}


public class TaskServiceImpl implements TaskService {

    @Override
    public void firstTask() {
        System.out.println(new Date()+":这是第一个定时任务");
    }

    @Override
    public void secondTask() {
        System.out.println(new Date() + ":这是第二个定时任务");
    }

}

三、如何使用Spring Task

      1) 纯xml配置的方式

            a.简单定时任务

                  实现业务功能+配置定时规则

<bean id="taskService" class="service.TaskServiceImpl"></bean>
    
    <!-- 配置定时任务 -->
    <task:scheduled-tasks>
    <!--
    ref:指定bean 
    method:指定bean中的方法
    initial-delay:服务器启动后多长毫秒开始执行
    fixed-delay:每隔多少毫秒执行
     -->
        <task:scheduled ref="taskService" method="firstTask" initial-delay="1000" fixed-delay="1000"/>
        <task:scheduled ref="taskService" method="secondTask" initial-delay="2000" fixed-delay="3000"/>
    </task:scheduled-tasks>

            b.复杂定时任务

      corn表达式:

 

<bean id="taskService" class="service.TaskServiceImpl"></bean>
    
    <!-- 配置定时任务 -->
    <task:scheduled-tasks>
        <task:scheduled ref="taskService" method="firstTask" cron="*/5 * * * * ?"/>
        <!-- 每天21点37分执行一次 -->
        <task:scheduled ref="taskService" method="secondTask" cron="0 37 21 * * ?"/>
    </task:scheduled-tasks>

      2)全注解的方式

    a. 在业务方法上提供注解

    b. 开启注解支持

    实现类:

@Service
public class TaskServiceImpl implements TaskService {

//    @Scheduled(initialDelay=1000, fixedDelay=1000)
    @Scheduled(cron="*/5 * * * * ?")
    @Override
    public void firstTask() {
        System.out.println(new Date()+":这是第1个定时任务");
    }

//    @Scheduled(initialDelay=2000, fixedDelay=3000)
    @Scheduled(cron="* 7 22 * * ?")
    @Override
    public void secondTask() {
        System.out.println(new Date() + ":这是第2个定时任务");
    }

}

    applicationContext.xml

    <!-- 扫描注解包路径 -->
    <context:component-scan base-package="service"></context:component-scan>
    
    <!-- 开启对@Scheduled注解的支持 -->
    <task:annotation-driven/>

四、在SpringBoot中实现定时任务

@Component
public class MyTask {
//    @Scheduled(initialDelay=1000, fixedDelay=1000)
    @Scheduled(cron="*/5 * * * * ?")
    public void firstTask() {
        System.out.println(new Date()+":这是第1个定时任务");
    }

//    @Scheduled(initialDelay=2000, fixedDelay=3000)
    @Scheduled(cron="* 25 23 * * ?")
    public void secondTask() {
        System.out.println(new Date() + ":这是第2个定时任务");
    }
}
@SpringBootApplication
@EnableScheduling
public class SpringBookTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBookTaskApplication.class, args);
    }

}
原文地址:https://www.cnblogs.com/hk-zsg/p/11456514.html