Spring整合Schedule定时任务详解

Spring 定时任务官方网站

一、概述

用Spring,就是为了简单。

但是我还是要总结下java定时任务实现的几种方式。

  • 1.TimerTask,等于一个线程隔一段时间运行一下。

  • 2.ScheduledExecutorService,线程池版的TimerTask。

  • 3.Spring支持的定时任务,@Schedule注解,支持crontab表达式。

  • 4.quartz,比较流行的任务调度工具,就是配置起来麻烦。

所以,这里还是先讲第三个吧,前两个跟Spring没关系,这里不讲,quartz配置麻烦,后面篇幅再说。

项目地址:
品茗IT-同步发布

品茗IT 提供在线支持:

一键快速构建Spring项目工具

一键快速构建SpringBoot项目工具

一键快速构建SpringCloud项目工具

一站式Springboot项目生成

Mysql一键生成Mybatis注解Mapper

如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

二、环境配置

本文假设你已经引入Spring必备的一切了,已经是个Spring项目了,如果不会搭建,可以打开这篇文章看一看《Spring和Spring Mvc 5整合详解》。

为方便使用,我们一般把定时任务的crontab表达式提出去。

所以,我们可以配置一个Spring的配置文件spring-schedule.xml,然后在Spring的主配置文件中,用<import resource="classpath*:spring-schedule.xml"/>引入即可,这样模块的耦合性就没那么强。

spring-schedule.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:annotation-config /><context:component-scan base-package="cn.pomit.springwork"></context:component-scan><bean id="annotationPropertyConfigurerSchedule"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="1" /><property name="ignoreUnresolvablePlaceholders" value="true" /><property name="locations"><list><value>classpath:schedule.properties</value></list></property></bean></beans>

schedule.properties:

schedule.task.test=0/2 * * * * ?

三、Schedule的配置

为方便使用,我们直接用注解来启用Scheduling。@EnableScheduling可以直接启用:

package cn.pomit.springwork.schedule.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;import cn.pomit.springwork.schedule.service.ScheduleService;@Configuration
@EnableScheduling
public class ScheduleConfig {@AutowiredScheduleService scheduleService;@Scheduled(cron = "${schedule.task.test}")public void dayJob() {scheduleService.doJob();}
}

这里的cron 表达式可以直接写到@Scheduled上,也可以用${schedule.task.test}这种方式去获取配置文件中的schedule.task.test属性。建议还是写配置文件,改起来方便。

其实这样已经可以用了。
如果你非要用配置文件,也不是不可以,Spring官网给你讲了,然后Spring官方文档还告诉你,如果只想支持@Scheduled注解,就可以不加@EnableAsync注解,所以这里就不加了,@EnableAsync是开启异步调用的。

官网给出的xml配置方式是这样的:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>

四、业务逻辑

ScheduleService:

package cn.pomit.springwork.schedule.service;import org.springframework.stereotype.Service;@Service
public class ScheduleService {public void doJob() {System.out.println("test");}}

快速构建项目

Spring组件化构建

SpringBoot组件化构建

SpringCloud服务化构建

喜欢这篇文章么,喜欢就加入我们一起讨论SpringBoot技术吧!

Spring整合Schedule定时任务详解相关推荐

  1. java 消息队列详解_Java消息队列-Spring整合ActiveMq的详解

    本篇文章主要介绍了详解Java消息队列-Spring整合ActiveMq ,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1.概述 首先和大家一起回顾一下Java 消息服 ...

  2. spring schedule定时任务详解

    spring schedule定时任务 文章目录 spring schedule定时任务 一.如何使用定时任务 1.启动类使用@EnableScheduling注解开启定时任务 2.方法使用@Sche ...

  3. Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! "看看星空,会觉得自己很渺小,可能我们在宇宙中从来就是一个偶然.所以,无论什么事情,仔 ...

  4. springboot2整合mysql5_SpringBoot2整合SSM框架详解

    SpringBoot2整合SSM框架详解 发布时间:2019-01-15 21:33, 浏览次数:1218 , 标签: SpringBoot SSM <>开发环境 * 开发工具:Eclip ...

  5. go mongodb排序查询_Kotlin与MongoDB整合CURD案例详解

    1.mongodb的低版本bson无法转换类型 比如MongoDB数据库表的字段类型为Decimal,实体类用String去定义就会报如下错误 No converter found capablof ...

  6. Spring包含JAR的详解

    一.Spring 常用包的说明 spring.jar :  包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and ...

  7. navicat创建MySql定时任务详解

    目录 navicat创建MySql 定时任务详解 一.开起定时任务 二.通过navicat创建定时任务 三.关闭.启动.别名.移动.删除event 四.查询Event信息 navicat创建MySql ...

  8. Springboot整合redis配置详解

    Springboot整合redis配置详解 1.导入依赖 <dependency><groupId>org.springframework.boot</groupId&g ...

  9. 二、SSM整合按步骤详解(清晰的思路加代码)从零开始一步步整合【二】(完结)

    接上一章节继续SSM整合按步骤详解(清晰的思路加代码)从零开始一步步整合[一] 上面讲了Spring和SpringMVC的整合,现在开始下一步,先来搭建一下MyBatis的环境 我们先来看看目录结构图 ...

最新文章

  1. 从“人脸”到“狗脸”,AI也要去宠物经济分杯羹?
  2. 深入理解DOM事件机制
  3. Replica small data to PostgreSQL from Oracle's Big table
  4. 实验7.3 字符串 7-5 查找指定字符
  5. aspxgridview 增加行号
  6. pytorch如何计算导数_PyTorch怎么用?来看这里
  7. python如何获取百度搜索结果的真实URL
  8. 测量怎么显示坐标_测量员必须掌握的——全站仪坐标放样
  9. oracle之完整性约束
  10. 利用anaconda给pycharm配置python3.7版本的tensorflow虚拟环境的配置
  11. HDU - 4704(费马小定理和快速幂)
  12. c++ 图片验证码识别_图片验证码识别方法
  13. 数字图像处理理论课件(清华大学计算机科学与技术)
  14. EdrawMax Ultimate v12.0 图表和流程图
  15. 银河麒麟linux找不到网卡,银河麒麟(Ubuntu)无法上网问题的解决方法
  16. netty权威指南(第二版)对应的源码
  17. powerbi python词云图_用Power BI制作词云
  18. 语料库mysql_基于PHP+MySQL的小型语料库程序设计解决方案
  19. java中 字符串的补位
  20. Encoder-Decoder模型

热门文章

  1. mockjs语法详解
  2. DDMS+AndroidStudio实现动态调试
  3. 随想daydayday
  4. git解决enter passphrase for key
  5. 堡垒机是干什么的? 看完这篇你就懂了
  6. 做外贸怎么跟进国外客户?怎么给国外客户电话?
  7. opentsdb java开发_安装openTSDB
  8. 【大数据Spark系列】Spark部署模式与作业提交
  9. 多人过河问题C语言贪心算法,南阳oj贪心算法之过河问题
  10. 【前端】等待异步任务js执行完毕再执行