`
Luob.
  • 浏览: 1584283 次
  • 来自: 上海
社区版块
存档分类
最新评论

spring 第13天 使用@scheduled注解执行定时任务

阅读更多
我们使用spring的注解 @Scheduled 执行定时任务
创建spring-task.xml 文件

<!---加入:xmlns:task="http://www.springframework.org/schema/task"-->
 
<?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:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/tx     
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-3.2.xsd    
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/task  
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
   ">  

<task:annotation-driven /> <!-- 定时器开关-->  
  
    <bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean>  
  
    <task:scheduled-tasks>  
        <!--  
            这里表示的是每隔五秒执行一次  
        -->  
        <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />  
        <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>  
    </task:scheduled-tasks>  
      
    <!-- 自动扫描的包名 -->    
    <context:component-scan base-package="com.spring.task" /> 




//编写我们的任务程序
public interface IMyTestService {  
       public void myTest();  
}  

@Component  //import org.springframework.stereotype.Component;  
public class MyTestServiceImpl  implements IMyTestService {  
      @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
      @Override  
      public void myTest(){  
            System.out.println("进入测试");  
      }  
} 



cron-like 表达式
字段 允许值 允许的特殊字符  
秒 0-59 , - * /  
分 0-59 , - * /  
小时 0-23 , - * /  
日期 1-31 , - * ? / L W C  
月份 1-12 或者 JAN-DEC , - * /  
星期 1-7 或者 SUN-SAT , - * ? / L C #  
年(可选) 留空, 1970-2099 , - * /  
表达式意义  
"0 0 12 * * ?" 每天中午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期间的每1分钟触发  
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发  
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发  
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发  
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发  
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发  
"0 15 10 15 * ?" 每月15日上午10:15触发  
"0 15 10 L * ?" 每月最后一日的上午10:15触发  
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发  
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发  
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发  
每天早上6点  
0 6 * * *  
每两个小时  
0 */2 * * *  
晚上11点到早上8点之间每两个小时,早上八点  
0 23-7/2,8 * * *  
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点  
0 11 4 * 1-3  
1月1日早上4点  
0 4 1 1 *  
分享到:
评论

相关推荐

    使用spring @Scheduled注解执行定时任务

    ### 使用Spring `@Scheduled` 注解执行定时任务 在现代软件开发中,特别是企业级应用领域,定时任务处理是一项常见的需求。例如,自动备份数据库、定时发送报告邮件、定期清理缓存等。Spring 框架自3.0版本起引入了...

    Spring Boot中的@Scheduled注解:定时任务的原理与实现

    ### Spring Boot中的@Scheduled注解:定时任务的原理与实现 #### 一、引言 在现代软件开发中,定时任务是一种非常常见的需求。无论是数据同步、定期清理缓存还是发送提醒邮件,都需要应用程序能够在特定的时间点...

    Spring @Scheduled定时任务动态修改cron参数

    `@Scheduled`注解是Spring Framework中用于创建定时任务的重要工具,它允许开发者在不重启应用的情况下,实现定时任务的动态配置,特别是修改cron表达式来调整执行周期。 在Spring中,定时任务主要通过`@Scheduled`...

    SpringBoot中使用@Scheduled注解创建定时任务的实现

    SpringBoot中使用@Scheduled注解创建定时任务的实现可以使用Timer、Quartz和@Scheduled注解三种方式来实现定时任务,每种方式都有其优缺。此外,使用@Scheduled注解实现定时任务的方式可以轻松地实现复杂的定时任务...

    Spring定时任务@Scheduled例子

    `@Scheduled`注解是Spring提供的一个强大工具,用于声明式地配置定时任务,无需编写复杂的线程管理和调度逻辑。在这个例子中,我们将深入探讨`@Scheduled`的用法以及与之相关的`task:scheduler`和`task:executor`。 ...

    spring boot @scheduled定时任务配置

    在Spring Boot框架中,`@Scheduled`注解是用于创建定时任务的重要工具,它使得开发者无需依赖外部的任务调度器如Quartz或CronJob,就能在应用内部轻松地实现周期性的任务执行。这个特性极大地简化了Java应用中的定时...

    spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法

    Spring Boot 中的定时任务是通过 @Scheduled 注解来实现的,该注解可以将方法标记为定时任务,Spring Boot 会自动发现并执行这些方法。@Scheduled 注解可以设置定时任务的执行时间、执行频率、延迟执行等参数。 在 ...

    详解Spring Boot中使用@Scheduled创建定时任务

    接下来,我们创建一个定时任务实现类,使用 @Scheduled 注解来定义需要定时执行的方法。 ```java @Component public class ScheduledTasks { private static final SimpleDateFormat sdf = new SimpleDateFormat(...

    @ScheduleTask注解实现定时任务,带分布式锁

    首先,`@ScheduleTask` 是一种模拟Spring框架中的 `@Scheduled` 注解的用法,用于标记方法作为定时任务。该注解通常包含参数如 `cron` 表达式,用于定义任务的执行时间。例如: ```java @ScheduleTask(cron = "0 0/...

    Spring boot如何通过@Scheduled实现定时任务及多线程配置

    Spring Boot 框架提供了多种方式来实现定时任务,包括使用 `@Scheduled` 注解和使用 Quartz 等第三方库。在本文中,我们将详细介绍如何使用 `@Scheduled` 注解来实现定时任务,并且探讨多线程配置的实现方式。 使用...

    spring @Scheduled定时任务代码

    spring @Scheduled定时任务代码

    @scheduled任务调度使用详解及@scheduled与多线程和@Async异步任务结合使用

    `@Scheduled`是Spring Framework中的一个注解,它允许我们在不需要任何第三方库的情况下,如Quartz或CronTrigger,就能创建定时任务。要使用`@Scheduled`,首先需要在配置类上启用定时任务支持,通过`@...

    spring注解Quartz定时执行功能

    利用Spring的`@Component`注解将该类注册为bean,再使用Quartz的`@DisallowConcurrentExecution`(防止并发执行)和`@PersistJobDataAfterExecution`(持久化任务数据)注解,以及Spring的`@Scheduled`注解来定义...

    SpringBoot执行定时任务@Scheduled的方法

    Spring Boot 提供了对定时任务的支持,通过使用 @Scheduled 注解,可以实现定时任务的执行。本文将详细介绍 Spring Boot 中 @Scheduled 定时器的使用。 创建定时任务 要使用 @Scheduled 注解,首先需要在项目启动...

    IDEA使用springboot自带scheduled实现任务调度

    在Java开发领域,Spring Boot框架以其便捷的特性深受开发者喜爱,而Spring Boot集成的Scheduled功能则为开发者提供了定时任务调度的能力。本篇文章将详细介绍如何在IDEA中利用Spring Boot的Scheduled来实现任务调度...

    Java课程实验 Spring Boot 任务管理(源代码+实验报告)

    1.在Spring Boot中,你可以使用@Scheduled注解来创建定时任务。将@Scheduled注解与方法一起使用,指定任务执行的时间表达式。 2.使用Spring的TaskScheduler: Spring提供了TaskScheduler接口和相关实现,用于任务...

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

    本文将详细介绍如何在Spring3中使用`@Scheduled`注解来创建计划任务。 首先,你需要创建一个Java类,该类中包含需要周期性执行的方法。这些方法需要添加`@Scheduled`注解,并且方法本身应无参数且无返回值。例如: ...

    Spring定时任务(Web项目)

    Spring的定时任务功能使得在Web项目中实现周期性任务变得轻松,无论是简单的定时执行还是复杂的调度需求,都有相应的解决方案。结合`@Scheduled`注解和`TaskScheduler`接口,开发者可以灵活地控制任务的执行时机和...

    spring 定时任务@Scheduled详解

    在Spring框架中,定时任务是通过`@Scheduled`注解实现的,该注解提供了灵活的方式来安排任务在特定时间执行。下面将详细讲解如何配置和使用`@Scheduled`,以及其相关的cron表达式。 首先,要启用Spring的定时任务...

    Spring-task定时任务

    - `@Scheduled`是Spring-task的核心注解,用于标记一个方法为定时任务。例如: ```java @Component public class ScheduledTasks { @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System....

Global site tag (gtag.js) - Google Analytics