Spring自带的定时调度工具Task

一.简介

    Spring有自己的调度定时工具,相对Quartz定时框架,它更为轻量级,使用起来也更加方便,当然它的功能并没有Quartz那么强大齐全,同时也有着自己的缺点,具体什么缺点在下面的内容介绍。使用起来有两种方式,一种是注解,一种是xml配置。接下来给大家一 一介绍。

二.使用--xml配置

    首先你要子自己的job项目中别写好自己功能性业务代码,这个就不多说了。接下来是你需要在自己的xml配置文件中写上这些配置。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

       <mvc:annotation-driven/>
       <context:component-scan base-package="com.cishoo.health.web.job"/>

       <task:scheduled-tasks>
              <task:scheduled ref="medRemindTask" method="execute" cron="0 30 0 * * ?"/>
       </task:scheduled-tasks>

</beans> 

    我的是用来将执行完的用药提醒在夜里面12:30进行刷新,状态由进行变为已经变为结束。ref="medRemindTask"就是你自己的实现功能的代码类。method="execute"就是你自己实现类里面的方法名。cron="0 30 0 * *",这个是cron表达式用来规定你的定时任务啥时候跑。具体语法简单,自己百度写出符合自己功能的cron表达式。


    这个是我的项目里面xml配置文件面相对的类和方法名。配置完就可以运行去测试,怎么样是不是很简单,自己赶快去试试吧。

三.使用--注解

    个人不喜欢这种方式,因为这个要一个个去加注解,当你有多个定时任务需要去修改时还要一个个去查找。不如直接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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

       <mvc:annotation-driven/>
       <context:component-scan base-package="com.cishoo.health.web.job"/>
       <task:annontation-driven>
</beans> 

    <task:annontation-driven>切记这个注解一定要加,不然添加的注解不被识别导致定时任务不会被执行。


    这是方法上的注解。

四.缺点

    虽然相对与Quartz框架来说,虽然配置和使用起来方便,但是没有任何事物是完美的。目前个人使用下来主要有以下几点。

    1.不能具体到特定的时间点去执行job任务。

扫描二维码关注公众号,回复: 2433795 查看本文章

    2.如果job挂掉,定时任务会抛出异常,就不会再次重新执行,所以每天都要去查看job是否跑过,不然会产生很多脏数据,会给开发人员带来很多麻烦。所以建议写一个手动修复数据的job以防万一。

    自己百度了一下貌似Quartz在抛出异常挂掉之后,会再重新开启job,再次运行。所以还是不要贪图简单,Quartz框架比较好,下一篇再给大家介绍Quartz框架的使用。

猜你喜欢

转载自blog.csdn.net/answerforwang/article/details/80060901
今日推荐