Spring调度

本文探讨了Spring调度的两种主要方式:Spring Task和Quartz。Spring Task作为Spring框架的一部分,提供轻量级的定时任务解决方案,而Quartz则允许更复杂的定时配置。文中详细解释了作业类、触发器的设置,并提供了Quartz和Spring Task的配置及使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调度方法

1)Java自带的java.util.Timer类,只能按照频度不能按照指定时间运行
2)使用Quartz,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂
3)Spring3.0以后自带的Task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多

作业类

  • 需要继承
    Quartz中需要继承org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中需要继承自java.util.TimerTask
  • 不需要继承
    普通类(推荐)

触发器

  • 每隔指定时间则触发一次
    在Quartz中对应的触发器为:-org.springframework.scheduling.quartz.SimpleTriggerBean
  • 每到指定时间则触发一次
    在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

Quartz普通类

1)添加jar包
无论继不继承,除了Spring的spring-context和spring-context-support还要添加quartz-all-1.6.0.jar ; jta.jar ;

2)定义做业

package com.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;



public class QuartzTest{

public void doJob(){
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}

}

3)配置作业类

<?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:p="http://www.springframework.org/schema/p"
    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">

<bean id="job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.quartz.QuartzTest"/><!-- 调度的对象 -->
</property>

<property name="targetMethod" value="doJob"/><!-- 调度的方法名 -->
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
</beans>

4)配置触发器

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

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

cronExpression即cron表达式,按“秒 分 时 日 月 星期 年”
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。

按顺序依次为
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
具体通配符请百度

5)配置调度工厂

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

6)将工程部署至tomcat或其他容器

Spring Task

  • 配置方式

1)pojo

package com.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;


@Component
public class QuartzTest{

public void doJob(){
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}

}

2)添加配置文件的命名空间以及描述

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=".......
       http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        ......."

3)配置文件

<!--扫描注解  -->
<context:component-scan base-package="com.quartz"/>
<!--@Component不指定名时默认类名小写  -->
<task:scheduled-tasks> 
        <task:scheduled ref="quartzTest" method="doJob" cron="* * 15 * * ?"/> 
</task:scheduled-tasks>

4)启动服务,Tomcat或者其他容器

  • 注解方式@Scheduled

1)Spring中的定义

@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scheduled
{
  public abstract String cron();

  public abstract long fixedDelay();

  public abstract long fixedRate();

  public abstract long initialDelay();
}

cron:指定cron表达式
fixedDelay:从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
fixedRate:从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

2)aopalliance.jar添加

3)pojo

package com.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class QuartzTest{
    @Scheduled(cron="* * 15 * * ?")
public void doJob(){
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}

}

4)添加配置文件的命名空间以及描述

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=".......
       http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd
        ......."

5)配置文件

<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven/>

<!--扫描注解  -->
<context:component-scan base-package="com.quartz"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值