spring定时任务

实现分类

  1. 1. Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。

  2. 2. 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。

  3. 3. Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍

用法说明

Quartz

  1. 继承org.springframework.scheduling.quartz.QuartzJobBean    

import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
import org.springframework.scheduling.quartz.QuartzJobBean;  
public class Job1 extends QuartzJobBean {  
  
private int timeout;  
private static int i = 0;  
//调度工厂实例化后,经过timeout时间开始执行调度  
public void setTimeout(int timeout) {  
this.timeout = timeout;  
}  
  
/** 
* 要调度的具体任务 
*/  
@Override  
protected void executeInternal(JobExecutionContext context)  
throws JobExecutionException {  
  System.out.println("定时任务执行中…");  
}  
}

2. 配置JobDetailBean

说明:JobDetailBean有两个属性,jobClass属性即我们在java代码中定义的任务类,jobDataAsMap属性即该任务类中需要注入的属性值

<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">  
    <property name="jobClass" value="com.gy.Job1" />  
    <property name="jobDataAsMap">  
        <map>  
            <entry key="timeout" value="0" />  
        </map>  
    </property>  
</bean>

3. 配置调度触发方式

Quartz的作业触发器有两种,分别是

org.springframework.scheduling.quartz.SimpleTriggerBean

org.springframework.scheduling.quartz.CronTriggerBean

第一种SimpleTriggerBean,只支持按照一定频度调用任务,如每隔30分钟运行一次。

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
    <property name="jobDetail" ref="job1" />  
    <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->  
    <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->  
</bean>

第二种CronTriggerBean,支持到指定时间运行一次,如每天12:00运行一次等

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
    <property name="jobDetail" ref="job1" />  
    <!—每天12:00运行一次 -->  
    <property name="cronExpression" value="0 0 12 * * ?" />  
</bean>

4. 配置调度工厂,说明:该参数指定的就是之前配置的触发器的名字

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    <property name="triggers">  
        <list>  
            <ref bean="cronTrigger" />  
        </list>  
    </property>  
</bean>

Quartz还有一种不用继承QuartzJobBean 实现的方式


Spring-task实现

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

1.编写pojo

mport org.springframework.scheduling.annotation.Scheduled;    
import org.springframework.stereotype.Component;  
  
@Component(“taskJob”)  
public class TaskJob {  
    @Scheduled(cron = "0 0 3 * * ?")  
    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
}

2. 添加task相关的配置

<?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:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
        http://www.springframework.org/schema/context   
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
    default-lazy-init="false">  
  
  
    <context:annotation-config />  
    <!—spring扫描注解的配置   -->  
    <context:component-scan base-package="com.gy.mytask" />  
      
    <!—开启这个配置,spring才能识别@Scheduled注解   -->  
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>



你可能感兴趣的:(定时任务)