SpringTask任务案例源码实现

时间:2022-05-10
本文章向大家介绍SpringTask任务案例源码实现,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

写在开始

一般来说,无论是生活中或者项目中都会用到定时任务。比如我自己来说,每天晚上写一篇博客,当然这个任务的时间点可能不是那么准时。更多的例子,比如我们项目中数据的统计,文件或者数据库的定时备份等等。

举个特别常见的例子,比如一些大型网站,首页的一些数据展示。有时候并不是实时展示的,可能是十分钟或者更久更新一次,来呈现给用户。

任务介绍

就目前自己接触的定时人来来说,这里简单的聊下一下三种:

1)java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。

2)开源调度框架Quartz,这是一个功能丰富比较强大的的调度器,可以实现内存和实例化数据库的任务(基于配置文件的内存实现和数据库实现)

3)spring3以后自带的task任务管理,可以将它看成一个轻量级的Quartz,相对来说使用起来更加方便。

功能实现

关于1,2任务这里不做介绍,后面会为大家详细介绍,这里主要介绍一下springTask的使用方法。

这里使用的spring4.0.6版本,是支持springTask的 ,需要依赖spring-context-4.0.6.RELEASE.jar和spring-context-support-4.0.6.RELEASE.jar等等,后面会有源码提供。

springTask提供了基于xml配置和注解的两种实现方式。

基于注解的实现TestJob.java:

package task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
 * 任务测试 科帮网 http://www.52itstyle.com/
 * 创建者        张志朋
 * 创建时间        2017年4月22日
 *
 */
@Component("TestJob")  
public class TestJob {
        @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 
    public void test1()
    {
        System.out.println("job1 开始执行");
    } 
        @Scheduled(cron = "0/5 * * * * ?")//每隔1秒隔行一次 
    public void test2()
    {
        System.out.println("job2 开始执行");
    } 
}

基于注解的实现spring-context.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:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

        <description>Spring Configuration</description>
        
        <context:component-scan base-package="task"/> 
        
        <!-- 配置任务线性池 -->
    <task:executor  id="executor" pool-size="10" /> 
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
</beans>

基于配置的实现TestJob.java:

package task;

import org.springframework.stereotype.Component;
/**
 * 任务测试 科帮网 http://www.52itstyle.com/
 * 创建者        张志朋
 * 创建时间        2017年4月22日
 *
 */
@Component("TestJob")  
public class TestJob {
    public void test1()
    {
        System.out.println("job1 开始执行");
    } 
    public void test2()
    {
        System.out.println("job2 开始执行");
    } 
}

基于配置的实现spring-context.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:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

        <description>Spring Configuration</description>
        
        <context:component-scan base-package="task"/> 
        
        <!-- 配置任务线性池 -->
    <task:executor  id="executor" pool-size="10" /> 
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
    <!-- 配置文件实现 如果使用配置文件实现 把注释解开即可然后 删除掉代码的注解-->
    <task:scheduled-tasks scheduler="scheduler">  
        <task:scheduled ref="TestJob" method="test1" cron="0/5 * * * * ?"/>  
        <task:scheduled ref="TestJob" method="test2" cron="0/6 * * * * ?"/>  
    </task:scheduled-tasks> 
</beans>

附: cronExpression的配置说明 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN-DEC , - * / 星期 1-7 或者 SUN-SAT , - * ? / L C # 年(可选) 留空, 1970-2099 , - * /

  • 区间
  • 通配符 ? 你不想设置那个字段

下面只例出几个式子

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触发

下载地址:http://www.52itstyle.com/thread-40036-1-1.html