Spring自带task定时任务工具使用

根据项目需求,需要一个定时任务每天执行一次,Spring 3.0之后自带了task调度工具,比Quartz更加的简单方便,相当于轻量级的Quartz,除spring核心包外不需要引入额外的jar包,支持注解和配置文件两种形式,特此介绍。

1. 配置文件方式

编写job类

package cn.com.job.order;

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

import org.springframework.stereotype.Component;

@Component("orderFileJob")
public class OrderFileJob {
	
	private static final long serialVersionUID = -7194794905986149036L;

	public void executeJob() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		System.out.println("executeJob........." + sdf.format(new Date()));
	}
}

spring配置文件设置

<?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:aop="http://www.springframework.org/schema/aop"
	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.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/aop 
   	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" 
    default-lazy-init="false">

	<context:component-scan base-package="cn.com.job.order" />
	
	<task:scheduled-tasks>
    	<task:scheduled ref="orderFileJob" method="executeJob" cron="0/10 * * * * ?"/> <!-- 测试,每10秒执行一次 -->
     </task:scheduled-tasks>
</beans>

添加beans标签 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" ;
设置spring注解扫描<context:component-scan base-package=“cn.com.job.order” />;
task标签的ref设置指定的job类,method设置要运行的方法,cron设置定时周期

2. 注解方式

编写job类,需要在运行方法上添加注解@Scheduled,并配置表达式

package cn.com.job.order;

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

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("orderFileJob")
public class OrderFileJob {
	
	private static final long serialVersionUID = -7194794905986149036L;

	@Scheduled(cron="0/10 * * * * ?")
	public void executeJob() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		System.out.println("executeJob........." + sdf.format(new Date()));
	}
}

spring配置文件设置

<?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:aop="http://www.springframework.org/schema/aop"
	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.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/aop 
   	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" 
    default-lazy-init="false">

	<context:component-scan base-package="cn.com.job.order" />
	
	<task:annotation-driven/>
</beans>

相比于第一种方式,采用注解方式只需在配置文件中添加<task:annotation-driven/>即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值