Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:
- 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下;
- 在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
分享到:
相关推荐
本文将详细探讨Spring Boot中@Scheduled注解的使用,包括其原理、实现流程、步骤和代码示例。通过本文,读者将能够了解如何在Spring Boot应用中轻松创建和管理定时任务。 # @Scheduled注解简介 在Spring框架中,@...
使用spring @Scheduled注解执行定时任务
spring boot @scheduled注解 cron 表达式实现计划任务调度。
本篇文章主要介绍了详解在Spring3中使用注解(@Scheduled)创建计划任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析,做了一些测试说明了各个参数的作用
本篇文章中主要介绍了Spring Boot中使用@Scheduled创建定时任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
在不停服务的情况下,动态修改Spring定时任务的执行周期,即动态修改定时任务的cron参数。
主要介绍了spring-boot通过@Scheduled配置定时任务,文中还给大家介绍了springboot 定时任务@Scheduled注解的方法,需要的朋友可以参考下
主要介绍了spring 定时任务@Scheduled的相关资料,文中通过示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
1.在Spring Boot中,你可以使用@Scheduled注解来创建定时任务。将@Scheduled注解与方法一起使用,指定任务执行的时间表达式。 2.使用Spring的TaskScheduler: Spring提供了TaskScheduler接口和相关实现,用于任务...
在Spring Boot中,注解起着至关重要的作用,它们被用来配置和组织代码,使其更加简洁、易于理解和维护。本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository...
Spring boot开启定时任务的三种方式 零、前言 第一种也就是最简单的一种:基于注解 (@Scheduled)的方式; 第二种:基于接口 (SchedulingConfigurer); 第三种:基于注解设定多线程定时任务。 一、基于@Scheduled注解...
老外的 spring scheduler 实例 使用注解,比较全比较实用
说明:@Scheduled 注解用于标注这个方法是一个定时任务的方法,有多种配置可选。cron支持cron表达式,指定任务在特定时间执行;fixedRate以特定频率执行任务;fixedRateString以string的形式配置执行频率。
本代码是个demo,展示spring定时任务的三种实现方式:xml配置实现,注解实现及动态定时任务实现。
- chapter4-1-1:[使用@Scheduled创建定时任务](http://blog.didispace.com/springbootscheduled/) - chapter4-1-2:[使用@Async实现异步调用](http://blog.didispace.com/springbootasync/) #### 日志管理 - ...
spring schedule 注解方式+xml 方式 实现的定时器功能
chapter4-1-1:使用@Scheduled创建定时任务 chapter4-1-2:使用@Async实现异步调用 chapter4-1-3:使用@Async实现异步调用:自定义线程池 chapter4-1-4:使用@Async实现异步调用:资源优雅关闭 chapter4-1-5:使用@...
springboot学习资料 ...8.1、使用@Scheduled创建定时任务 9 8.2、使用@Async实现异步调用 9 8.3、自定义参数 9 8.4、多环境配置 9 8.5、修改端口号 9 8.6、SpringBoot yml 使用 9 8.7、发布打包 9