Spring整合Schedule定时任务详解

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/beans
                    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                    http://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 {
	@Autowired
	ScheduleService 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技术吧!
品茗IT交流群

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值