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任务。

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring 定时任务是 Spring 框架提供的一种定时执行任务的方法,可以在指定的时间间隔或者指定的时间点,执行某个任务。Spring 定时任务的实现依赖于 Spring 自带TaskExecutor、TaskScheduler 和 Cron 表达式。 Spring 定时任务的步骤如下: 1. 在配置文件中配置 TaskExecutor 或 TaskScheduler。 2. 创建一个带有 @Scheduled 注解的方法,指定该方法的执行时间或时间间隔。 3. 启动应用程序。 4. Spring 定时任务会自动执行指定时间或时间间隔的方法。 下面是一个简单的 Spring 定时任务的示例: ```java @Component public class MyTask { @Scheduled(fixedRate = 1000) // 每隔 1 秒执行一次 public void task() { System.out.println("定时任务执行中..."); } } ``` 在上面的代码中,我们使用 @Scheduled 注解来标记一个定时执行的方法,并指定了该方法执行的时间间隔为 1 秒。需要注意的是,使用 @Scheduled 注解的类必须被 Spring 扫描到,可以通过 @Component 标记该类。 除了使用 fixedRate 属性来指定执行时间间隔之外,@Scheduled 注解还有其他属性可以使用,例如 fixedDelay、initialDelay 和 cron 等,可以根据实际需求灵活配置。 在配置 TaskExecutor 或 TaskScheduler 时,可以选择使用 Spring 自带的 ThreadPoolTaskExecutor 和 ConcurrentTaskScheduler,也可以自定义实现。需要注意的是,如果使用默认的线程池和任务调度器,需要在配置文件中添加 @EnableScheduling 注解才能生效。 以上就是 Spring 定时任务的基本使用方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值