当前位置: 首页 > news >正文

Spring任务调度之Spring-Task

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

一、第一种:配置文件方式

第一步:编写作业类 

  即普通的pojo,如下:

import org.springframework.stereotype.Service; 
@Service 
public class TaskJob { 
       
    public void job1() { 
        System.out.println(“任务进行中。。。”); 
    } 
}

第二步: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">


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


</beans>

说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式。<context:component-scan base-package="com.gy.mytask" />这个配置不多说了,spring扫描注解用的。


 

二、第二种:使用注解形式

也许我们不想每写一个任务类还要在xml文件中配置下,我们可以使用注解@Scheduled,我们看看源文件中该注解的定义:

@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(); 
}

 

可以看出该注解有三个方法或者叫参数,分别表示的意思是

 

  cron:指定cron表达式。

  fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从                       上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。

  fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务                        开始到下一个任务开始的间隔,单位是毫秒。

第一步:编写pojo

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

说明:理论上只需要加上<task:annotation-driven />这句配置就可以了,这些参数都不是必须的。

配置完毕,spring task还有很多参数,具体参考xsd文档 http://www.springframework.org/schema/task/spring-task-3.0.xsd

 

参考文章:https://www.cnblogs.com/hongwz/p/5642497.html

转载于:https://www.cnblogs.com/java-jun-world2099/articles/10164649.html

相关文章:

  • P5112 FZOUTSY
  • java B2B2C springmvc mybatis多租户电子商城系统- 路由定位器
  • linux对vxlan的支持
  • Mysql优化
  • 3.1Python的判断选择语句
  • 深度解析ES6通过WeakMap解决内存泄漏问题
  • Redis 和 memcache 简单比较
  • verilog语法实例学习(1)
  • Docker三剑客之docker-machine
  • 正者表达式exec和match
  • Linux操作系统有什么吸引力,在程序员中这么受欢迎!
  • Oracle常用语句
  • Ubuntu Vscode安装
  • wx2tt 微信小程序转头条小程序工具
  • Min_25筛
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • C# 免费离线人脸识别 2.0 Demo
  • exports和module.exports
  • happypack两次报错的问题
  • node-glob通配符
  • Puppeteer:浏览器控制器
  • Ruby 2.x 源代码分析:扩展 概述
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 那些被忽略的 JavaScript 数组方法细节
  • 少走弯路,给Java 1~5 年程序员的建议
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 使用parted解决大于2T的磁盘分区
  • 通过npm或yarn自动生成vue组件
  • 学习JavaScript数据结构与算法 — 树
  • 源码安装memcached和php memcache扩展
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • $ git push -u origin master 推送到远程库出错
  • $(function(){})与(function($){....})(jQuery)的区别
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (42)STM32——LCD显示屏实验笔记
  • (6)添加vue-cookie
  • (多级缓存)多级缓存
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (算法二)滑动窗口
  • (转)关于多人操作数据的处理策略
  • .Net Web项目创建比较不错的参考文章
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .NET实现之(自动更新)
  • @Async注解的坑,小心
  • @Autowired @Resource @Qualifier的区别
  • @require_PUTNameError: name ‘require_PUT‘ is not defined 解决方法
  • @Transactional 竟也能解决分布式事务?
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [acm算法学习] 后缀数组SA