spring基于task标签和通过xml配置实现任务的区别

spring3.x可以通过<task>标签轻易的定义定时任务,而且不需要依赖第三方jar包如quartz等,这个是spring自己的实现,但不支持集群,其cron表达式也不支持年。
我们可以简单的通过以下配置:


<?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:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>

<context:component-scan base-package="com.xls" />

<!-- 开启@AspectJ AOP代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />

<!-- 任务调度器 -->
<task:scheduler id="scheduler" pool-size="10" />

<!-- 任务执行器 -->
<task:executor id="executor" pool-size="10" />

<!--开启注解调度支持 @Async @Scheduled -->
<task:annotation-driven executor="executor"
scheduler="scheduler" proxy-target-class="true" />

</beans>




/**
*
*/
package com.xls.task;

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

/**
*
*
* @Description:
* @Author: XiongLiangSheng
* @CreateDate: 2014年9月28日 下午2:09:50
* @Version: v1.0
*
* Date CR/DEFECT Modified By Description of change
*/
@Component
public class SpringTaskDemo {
@Scheduled(fixedDelay = 5000)
void doSomethingWithDelay(){
System.out.println("I'm doing with delay now!");
}

@Scheduled(fixedRate = 5000)
void doSomethingWithRate(){
System.out.println("I'm doing with rate now!");
}

@Scheduled(cron = "0/5 * * * * *")
void doSomethingWith(){
System.out.println("I'm doing with cron now!");
}
}


就可以定义好定时任务。

测试类:

/**
*
*/
package com.xls.task;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
*
* @Description:
* @Author: XiongLiangSheng
* @CreateDate: 2014年9月28日 下午2:29:22
* @Version: v1.0
*
* Date CR/DEFECT Modified By Description of change
*/
public class TestTask {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:quartz.xml");


}

}


运行测试类我们可以看到定时任务已经在运行了。但是如果在配置文件中加上default-lazy-init="true",测试类主线程会立即结束,似乎没有发现有定时任务Scheduler。但是如果通过xml文件配置quartz任务则不会有这种现象,但项目必须引入quartz相关的jar包,附件是两种方式实现的定时任务。其区别有待查证。

转:
Spring3.0以后,自己已经完全支持更加精确的时间,而不需要Quartz(Quartz是一个开放源码项目,专注于任务调度器,提供了极为广泛的特性如持久化任务,集群和分布式任务等。Spring对Quartz的集成与其对JDK Timer的集成在任务、触发器和调度计划的声明式配置方面等都非常相似。 )的支持:当然后面我们也会用Quartz实现任务的调度。

Spring3.0同样也使用cron表达式。与Quartz不同的是,Spring3.0不支持年,而Quartz支持年。但这点好象并不是非常重要。

cron表达式:-是用空格分开的时间字段,不使用年。
*(秒0-59)
*(分钟0-59)
*(小时0-23)
*(日期1-31)
*(月份1-12或是JAN-DEC)
*(星期1-7或是SUN-SAT)

示例:
*/5 * * * * 6-7 :: 每个周6到周日,每隔5秒钟执行一次。

*/1 * * 7-9 1-2 1-7 :: 1月到2月中的7号到9号,且必须要满足周一到周日,每隔1秒钟执行一次。

*/1 * * 7-9 1,5 1-7 :: 注意里面的,(逗号),只有1月和5月的7到9号,且必须要满足周一到周日,每一秒钟执行一次。

*/1 17-59 * 7-9 1,5 1-7 :: 只解释17-59,是指从第17分钟到第59分钟,在指定的时间内,每一秒种执行一次

* 17-59 * 7-9 1,5 1-7 :: 此代码的功能与上面完全相同。如果不写秒即为每一秒执行一次。

59 19-23 * 7-9 1,5 1-7 :: 19分-23分的每59秒钟时只执行一次。

59 19,26 * 7-9 1,5 1-7 :: 注意里面的,(逗号),是指只有19分或是26分的56秒钟时执行一次。

* * 16-23 7-9 1,5 1-7 :: 定义每天的16点到23点每一秒钟执行一次。

59 59 23 * * 1-5 :: 定义每周1到周5,晚上23:59:59秒只执行一次。
这个相当用有。可以工作时间每天给用户发邮件。


在Spring3.0中引用了新的命名空间-task:
task:scheduler 用于定义一个ThreadPoolTaskScheduler,并可以指定线程池的大小,
即pool-size.所有任务队列都将会在指定大小的线程池中运行:

定义如下:
<!-- 对于同一个Pojo可以声明多次,并设置标记属性 -->
<bean id="one" class="cn.itcast.schedule.One">
<property name="task" value="A"></property>
</bean>

<bean id="two" class="cn.itcast.schedule.One">
<property name="task" value="B"></property>
</bean>

<bean id="three" class="cn.itcast.schedule.One">
<property name="task" value="C"></property>
</bean>

<!-- 声明一个具有两个线程的池,每一个对象将获取同样的运行机会 -->
<task:scheduler id="sch" pool-size="2"/>

<!-- 引用线程池 -->
<task:scheduled-tasks scheduler="sch">
<!-- 引用Spring Bean并设置调用的方法的时间间隔 -->
<task:scheduled ref="one" method="doSomeThing" fixed-delay="#{1000*3}"/>
<task:scheduled ref="two" method="doSomeThing" fixed-delay="#{1000*3}"/>
<task:scheduled ref="three" method="doSomeThing" fixed-delay="#{1000*3}"/>
</task:scheduled-tasks>

<!-- 配置一个定时执行的任务 -->
<bean id="work" class="cn.itcast.schedule.Two"/>

<task:scheduler id="sendMail"/>

<task:scheduled-tasks scheduler="sendMail">
<!-- 定义在1月8号19:37:1秒执行一次,无论是周几 -->
<task:scheduled ref="work" method="work" cron="1 37 19 8 1 *"/>
</task:scheduled-tasks>


定义好之后,正常启动容器即可,只有条件符合,即会按要求执行任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值