轻松实现定时调度[Spring Task + Cron]

来源:互联网 发布:厦门第二世界网络 编辑:程序博客网 时间:2024/05/18 03:05

使用Quartz组件完成定时调度特别麻烦,一定要换成Spring Task

定义一个任务的执行类

package zzu.mzy.util ;import java.text.SimpleDateFormat;public class MyTaskA { //不需要去继承任何的父类    public void runJob() {        System.out.println("当前日期时间" + new SimpleDateFormat("yyyy-MM-dd        HH:mm:ss.SSS").format(new java.util.Date()));    }}

用Spring Task完成一般有两种形式:
1、xml文件配置
2、注解配置

1.基于配置文件:

修改applicationContext.xml文件

需要追加task处理的命名空间定义

xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.1.xsd

此时已提供好了该文件的原始文件Schema,将其配置到XML编译环境之中,随后进行task操作的配置,首先配置间隔触发

<bean id="myTask" class="zzu.mzy.util.MyTaskA"/><task:scheduled-tasks> <!-- 需要配置要执行的作业内容 --><task:scheduled ref="myTask" method="runJob" fixed-rate="2000"/></task:scheduled-tasks>

使用Cron定时触发

<bean id="myTask" class="zzu.mzy.util.MyTaskA"/><task:scheduled-tasks> <!-- 需要配置要执行的作业内容 --><!--<task:scheduled ref="myTask" method="runJob" fixed-rate="2000"/>--><task:scheduled ref="myTask" method="runJob" cron="* * * * * ?"/></task:scheduled-tasks>

2.基于Annotation:

修改applicationContext.xml

<?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"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><task:annotation-driven/><context:annotation-config/><context:component-scan base-package="zzu.mzy"/></beans>

配置间隔触发

package zzu.mzy.util;import java.text.SimpleDateFormat;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Component // 标记出这是一个交由Spring管理的JavaBean类public class MyTaskB { // 不继承任何的父类@Scheduled(cron="* * * * * ?") // 设置为每秒一触发    public void runJob() {        System.out.println("【MyTaskB - 当前日期时间】" + new SimpleDateFormat("yyyy-MM-dd        HH:mm:ss.SSS").format(new java.util.Date()));    }}

当然,如果有多个定时任务,为避免差错,一定不能让他们顺序执行,最好是

建立一个调度池

使所有的任务不再是单线程的形式完成,而是多线程

<task:annotation-driven/><!-- 定义了一个任务调度池,这个池可以同时并行执行20个任务 --><task:scheduler id="schedulerPool" pool-size="20"/>

这样,基于Spring Task + Cron实现的任务调度就完成了,Spring对于定时任务的处理支持要比原始的QuartZ组件更加方便,但还是有很多人愿意用QuartZ,真的搞不懂唉

1 0