Spring-Task实现定时任务

上一篇中,spring+Quartz的配置还是挺复杂的,因为Quartz其实是个重量级的工具,如果我们只是想简单的执行几个简单的定时任务,推荐使用Spring-task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式。

注:使用spring-task时,是spring3.0以上的版本
前提:spring相关配置已完成
一定要配置spring监听

第一种:使用配置文件的形式

1.编写工作类,普通的Java类即可

package test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
 public void test1(){
    	System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    	System.out.println("测试:作业类的test1方法");
	}
}

2.配置spring-task.xml文档

<beans xmlns="http://www.springframework.org/schema/beans"
		...
 	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.2.xsd
	...
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
	">

		<bean id="test" class="test.Test"/>
		 <task:scheduled-tasks> 
		 		<!--每隔5秒执行1次-->
	         	<task:scheduled ref="test" method="test1" cron="*/5 * * * * ?"/> 
	     </task:scheduled-tasks> 
</beans>

3.配置运行测试,结果如下

在这里插入图片描述

注意:
1.使用 < task:scheduled-tasks >标签时,注意在beans中指定task

	xmlns:task="http://www.springframework.org/schema/task"
	
	http://www.springframework.org/schema/task
	http://www.springframework.org/schema/task/spring-task-3.0.xsd

2.将spring-task单独配置在一个xml中,否则会导致重复调用,因为spring启动会调用一次,任务调度再调用一次,会导致重复执行的现象
3.案例中使用的是每隔5秒执行一次,通过修改cron属性值可以设置为固定时间,比如:

	<task:scheduled ref="test" method="test1" cron="0 0 12 * * ?" />    每天中午十二点触发 
	ref 参数指定的是任务类,method 指定的是需要运行的方法
	cron表达式:其他见[附]
第二种:使用注解

1.编写工作类,普通的Java类(同上)
2.配置spring-task.xml文档

<context:component-scan base-package="test"/><!--指定注解-->
<task:annotation-driven /><!--开启task注解-->

注意:如果Spring boot项目,没有XML,在启动类处使用注解

@EnableScheduling//自动扫描任务类·

3.在工作类上使用注解@Scheduled

package test;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("test")
public class Test {
	@Scheduled(cron = "*/5 * * * * ?")//每隔5秒执行一次
	public void test1(){
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		System.out.println("测试:作业类的test1方法");
	}
}

4.运行测试,结果如下
在这里插入图片描述

注:
1.配置xml同上,单独配置,注意重复调用
2.@Scheduled注解参数:

	cron:指定cron表达式。
   	fixedDelay:从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
   	fixedRate:从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

【附】
cron表达式

每一位表示的是 [ 秒 分 时 日 月 周 年]
字段	    允许值	   		允许的特殊字符
秒   	 0-59   	  		 - * /
分   	 0-59   	   		 - * /
小时   	 0-23          	  	 - * /
日期    	 1-31    	 	     - * ? / L W C
月份     1-12 或者 JAN-DEC    - * /
星期     1-7 或者 SUN-SAT     - * ? / L C #
年(可选) 留空, 1970-2099     - * / 

-  区间  
*  通配符  
? 你不想设置那个字段

例子:
CRON表达式    				含义 
"0 0 12 * * ?"    			每天中午十二点触发 
"0 15 10 ? * *"    			每天早上10:15触发 
"0 15 10 * * ?"    			每天早上10:15触发 
"0 15 10 * * ? *"   		每天早上10:15触发 
"0 15 10 * * ? 2005"    	2005年的每天早上10:15触发 
"0 * 14 * * ?"   			每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"   		    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    		每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    		每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    	三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    	每个周一、周二、周三、周四、周五的10:15触发
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值