spring schedule定时任务(一):注解的方式

简介: 我所知道的java定时任务的几种常用方式: 1、spring schedule注解的方式; 2、spring schedule配置文件的方式; 3、java类继承TimerTask; 第一种方式的实现: 1、使用maven创建spring项目,schedule在spring-context.jar的包下边,因此需要导入与之相关的包;同时,我配的是spring w
我所知道的java定时任务的几种常用方式:

1、spring schedule注解的方式;
2、spring schedule配置文件的方式;
3、java类继承 TimerTask;

第一种方式的实现:

1、使用maven创建spring项目,schedule在spring-context.jar的包下边,因此需要导入与之相关的包;同时,我配的是spring web项目,也同时导入了spring-web和spring-webmvc的包,如下:
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

maven导包配置只需要如下就好,其他相关的依赖,maven会自行解决。

2、配置spring项目的基础文件spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
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
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">

    <!-- 开启定时任务 -->
    <task:annotation-driven />
    <!-- 开启注解 -->
    <context:annotation-config />
    <!-- 指定相关的包路径 -->
    <context:component-scan base-package="scheduleTest"/>

</beans>


3、编写java业务代码,需要在类声明上边添加 @Component注解,并在需要定时任务执行的方法声明上添加@Scheduled(cron = "0/5 * * * * ?")注解以及相关的参数。
参数使用可以参考 http://blog.csdn.net/isnotsuitable/article/details/7464556,我示例中表示每五秒执行一次
package scheduleTest;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * spring定时器1
 * 
 * @author tuzongxun123
 *
 */
@Component
public class ScheduleTest {

    @Scheduled(cron = "0/5 * * * * ?")
    public void schTest1() {
        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sim.format(date);
        System.out.println("这是spring定时器1,每五秒执行一次,当前时间:" + dateStr);
    }
}


4、web项目的基础配置文件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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>appversion</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <description>spring监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


上边的配置中使用了spring的上下文监听器,在这种情况下项目启动后,spring.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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>appversion</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value> 
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这里是吧spring的监听器换成了mvc的调度器,在调度器加载的时候就运行spring.xml中的定时任务配置,因此启动项目后看到效果一样。如果把上边的监听器和mvc的调度器一起配在这里,会看到启动项目后同一时间内这个定时任务需要执行的业务会执行两次。
目录
相关文章
|
1月前
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
3月前
|
Java 调度 Maven
Spring Task 自定义定时任务类
Spring Task 自定义定时任务类
36 0
|
1月前
|
Java BI 调度
Spring Boot 整合xxl-job实现分布式定时任务
XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 xxl是xxl-job的开发者大众点评的许雪里名称的拼音开头。
|
2月前
spring3.0定时任务
spring3.0定时任务
|
2月前
|
监控 Java 测试技术
Spring Boot和XXL-Job:高效定时任务管理
Spring Boot和XXL-Job:高效定时任务管理
58 0
|
4月前
|
Java Spring
定时任务schedule(spring boot )
定时任务schedule(spring boot )
48 0
|
4月前
|
Java Linux 调度
Java【付诸实践 03】Spring定时任务注解@Scheduled+@EnableAsync用法详解(简单说明+应用场景+demo源代码+执行过程分析)
Java【付诸实践 03】Spring定时任务注解@Scheduled+@EnableAsync用法详解(简单说明+应用场景+demo源代码+执行过程分析)
52 2
|
4月前
|
Java 调度 Docker
Spring Boot 3 整合 xxl-job 实现分布式定时任务调度,结合 Docker 容器化部署(图文指南)
Spring Boot 3 整合 xxl-job 实现分布式定时任务调度,结合 Docker 容器化部署(图文指南)
Spring Boot 3 整合 xxl-job 实现分布式定时任务调度,结合 Docker 容器化部署(图文指南)
|
9月前
|
Java Spring
设置 Spring Boot 的定时任务线程池以优雅退出
设置 Spring Boot 的定时任务线程池以优雅退出
186 0
|
7月前
|
XML 监控 Java
spring定时任务Spring Scheduler
spring定时任务Spring Scheduler
47 0