Java 定时任务系列(2)-Spring 定时任务的几种实现

本文来自网络一些博客的整理(包括gong1208的博客 dary1715的博客)
1、简介
这个系列介绍Spring框架实现定时任务的两种方式以及一些高级的用法,包括:
1、使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。
2、Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。

2、Quartz的使用
作业类继承自特定基类的方式: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("定时任务执行中…");
    }
}

②spring配置文件中配置作业类JobDetailBean

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

说明:org.springframework.scheduling.quartz.JobDetailBean有两个属性,jobClass属性即我们在java代码中定义的任务类,jobDataAs

③配置作业调度的触发方式(触发器)
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="job2" />
    <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="job2" />
    <!—每天12:00运行一次 -->
    <property name="cronExpression" value="0 0 12 * * ?" />
</bean>

④配置调度工厂

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

⑤启动你的应用即可,即将工程部署至tomcat或其他容器。
到此,spring中Quartz的基本配置就介绍完了,当然了,使用之前,要导入相应的spring的包与Quartz的包,这些就不消多说了。

其实可以看出Quartz的配置看上去还是挺复杂的,没有办法,因为Quartz其实是个重量级的工具,如果我们只是想简单的执行几个简单的定时任务,有没有更简单的工具 请看我第下文Spring task的介绍。
特别的,关于CronExpression 的详细用户,请参考dary1715的博客

3、Spring Task
上节介绍了在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级
的Quartz,而且使用起来很简单,除spring相 关的包外不需要额外的包,而且支持注解和配置文件两种
形式,下面将分别介绍这两种方式。

配置文件方式:

①配置作业类
即普通的pojo,如下:

import org.springframework.stereotype.Service;
@Service
public class TaskJob {

    public void job1() {
        System.out.println(a??task exe...a??);
    }
}

②在spring配置文件头中添加命名空间及描述

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

③spring配置文件中设置具体的任务

<task:scheduled-tasks> 
    <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/> 
</task:scheduled-tasks>
<context:component-scan base-package=" com.gy.mytask " />

说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式,具体写法这里不介绍了,详情见上篇文章附录。

这个配置不消多说了,spring扫描注解用的。

到这里配置就完成了,是不是很简单。

使用注解形式:

① 编写普通的POJO

import 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(“任务进行中。。。”);
    }
}

②添加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"/>

其他关于Spring Task使用的例子请参照 参考链接1 参考链接2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值