ITeye博客

在Spring3中使用注解(@Scheduled)创建计划任务

zywang · 2011-03-09 17:35 · Spring · 企业架构 · Markdown

Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:

  1. 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下;
  2. 在Spring配置文件中添加三个<task:**** />节点;

最后说明一下,第一步创建的Java类要成为Spring可管理的Bean,可以直接写在XML里,也可以@Component一下

 

示例如下

计划任务类:

/**
 * com.zywang.spring.task.SpringTaskDemo.java
 * @author ZYWANG 2011-3-9
 */
package com.zywang.spring.task;

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

/**
 * Spring3 @Scheduled 演示
 * @author ZYWANG 2011-3-9
 */
@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!");
	}
}

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:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
	<!-- Enables the Spring Task @Scheduled programming model -->
	<task:executor id="executor" pool-size="5" />
	<task:scheduler id="scheduler" pool-size="10" />
	<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>

 

 以上内容基于Spring 3.0.5 版本运行,参考文档为spring-framework-reference-3.0.5.pdf

评论(10)

  1. #10 楼 · sanorol · 2017-10-11 00:30

    qja writes:

    [quote=citi007] <task:annotation-driven executor="executor" scheduler="scheduler" /> 这是否与spring 事物声明式事物管理的配置有冲突?

    <tx:annotation-driven transaction-manager="transactionManager"/>

    我在整合的时候报出异常 Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.

    猜想Only one AsyncAnnotationBeanPostProcessor may exist within the context, 而不同的annotation-drivern 必然会多次创建new AsynoAnnotationBean后处理器,

    你有声明式的整合方案么? mail to :citi007@126.com[/quote] 这个问题最后怎么解决的呢?

    最后解决,去掉 annotation-drivern ,直接加了注解会自动扫描创建

  2. #9 楼 · qja · 2017-01-20 17:32

    citi007 writes:

    <task:annotation-driven executor="executor" scheduler="scheduler" /> 这是否与spring 事物声明式事物管理的配置有冲突?

    <tx:annotation-driven transaction-manager="transactionManager"/>

    我在整合的时候报出异常 Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.

    猜想Only one AsyncAnnotationBeanPostProcessor may exist within the context, 而不同的annotation-drivern 必然会多次创建new AsynoAnnotationBean后处理器,

    你有声明式的整合方案么? mail to :citi007@126.com

    这个问题最后怎么解决的呢?

  3. #8 楼 · zhuchao_ko · 2013-01-11 20:48

    :idea: 不错 这个DEMO终于完整了。

  4. #7 楼 · langke93 · 2012-11-05 12:02

    <context:component-scan base-package="*" />
    把这给漏了

  5. #6 楼 · violetluna · 2011-12-28 11:15

    xml声明差这个 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd

  6. #5 楼 · itbailu · 2011-12-09 11:58

    没反映啊 :shock: :shock: :shock: :shock:

  7. #4 楼 · runjia1987 · 2011-11-26 00:41

    这个任务调度注解 需要quartz这个库支持吗?

  8. #3 楼 · zywang · 2011-05-19 17:11

    citi007 writes:

    在使用定时任务的annotation-driven时,需要配置 <task:annotation-driven executor="executor" scheduler="scheduler" /> 这是否与spring 事物声明式事物管理的配置

    我们现在的系统中没有出现问题,你检查下,有没有类被重复加载啊

  9. #2 楼 · citi007 · 2011-05-19 10:18

    <task:annotation-driven executor="executor" scheduler="scheduler" /> 这是否与spring 事物声明式事物管理的配置有冲突?

    <tx:annotation-driven transaction-manager="transactionManager"/>

    我在整合的时候报出异常 Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.

    猜想Only one AsyncAnnotationBeanPostProcessor may exist within the context, 而不同的annotation-drivern 必然会多次创建new AsynoAnnotationBean后处理器,

    你有声明式的整合方案么? mail to :citi007@126.com

  10. #1 楼 · citi007 · 2011-05-19 10:07

    在使用定时任务的annotation-driven时,需要配置 <task:annotation-driven executor="executor" scheduler="scheduler" /> 这是否与spring 事物声明式事物管理的配置

评论功能维护中,当前仅展示历史评论。