使用Spring Task开发定时任务

作者: xiaozhu 发布时间: 2022-05-15 浏览: 1010 次 编辑

一、定时任务概述

在项目中开发定时任务应该一种比较常见的需求了,在Java中开发定时任务主要有三种解决方案:1 使用JDK自带的Timer 2 使用第三方组件Quartz 3 使用Spring Task。Timer是JDK自带的定时任务工具,其简单易用,但是对于复杂的定时规则无法满足在实际项目开发中也很少使用到,Quartz功能强大但是使用起来相对笨重,而Spring Task则具备前两者的优点(功能强大且简单易用),在这篇博客中我将介绍如何使用Spring Task进行定时任务开发。

二、使用Spring Task实现定时任

Spring Task开发定时任务有两种任务配置方式:1 XML配置 2 注解配置。我将绍如何使用XML进行任务配置再介绍如何使用注解配置。

使用XML配置方式开发定时任务

  1. 创建Maven WEB项目

打开Eclipse创建maven WEB项目将开发所需要的jar的坐标配置在pom中。以下是我项目中的POM文件。

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
</dependencies>
2 在webapp文件中创建WEB-INF并在该文件夹下添加web.xml文件,web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springTask</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

3 在项目src目录下创建任务Service以及对应的实现类,代码对应如下:

public interface TaskService {

public void taskOne();

public void taskTwo();

}
public class TaskServiceImpl implements TaskService {

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

}

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

}

}

4 在maven项目的resource目录下配置定时任务

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
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:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<bean id="taskService"
class="com.wistronits.service.impl.TaskServiceImpl"></bean>

<!-- 配置定时规则 -->
<task:scheduled-tasks>
<!-- 表示在服务器启动一秒后执行任务1 并且每隔1秒钟执行一次-->
<task:scheduled ref="taskService" method="taskOne" initial-delay="1000" fixed-delay="1000" />
<!-- 表示在每天的13:40分0秒执行该任务->
<task:scheduled ref="taskService" method="taskTwo" cron="0 40 13 * * ?"/>
</task:scheduled-tasks>
</beans>

5 执行结果如下截图所示:

使用注解方式开发定时任务

使用注解方式开发定时任务与xml大体相同,不同于xml的差别如下:

1 在xml不在配置Spring管理的bean,而是配置Spring的包的扫描路径以及启用定时任务注解。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache="http://www.springframework.org/schema/cache"
    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:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
 
<context:component-scan base-package="com.wistronits.service"></context:component-scan>
<task:annotation-driven/>    
</beans>
@Service
public class TaskServiceImpl implements TaskService {
 
    @Override//表示服务器启动两秒后启动该任务每隔一秒钟执行一次
    @Scheduled(initialDelay=2000,fixedDelay=1000)
    public void taskOne() {
        System.out.println("这是我的第一个定时任务"+new Date());
         
    }
 
    @Override
    @Scheduled(cron="*/5 * * * * ?")//每隔五秒钟执行一次
    public void taskTwo() {
        System.out.println("这是我的第二个定时任务"+new Date());
         
    }
 
}

2 在需要进行定时任务的方法上中增加定时任务注解