• Spring定时任务执行


    备注:这个是基于搭建好spring的环境下的

    注解方式:

    1.定时任务类

    package com.test;
    import java.util.Date;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;

    @Component("mytask")
    public class MyTask {
        int i =0;
        @Scheduled(cron = "0 0/1 * * * ?")//!!!!!表示每分钟执行一次!!!!!cron是表示触发的时间表达式
        public void doTask(){
            System.out.println("结果是:"+(i++)+":"+new Date());
        }
    }

    2.配置文件:schedule.xml;备注:名称随便起。还有,需要在总的配置文件映入改文件

    <?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:task="http://www.springframework.org/schema/task" 
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
           xmlns:aop="http://www.springframework.org/schema/aop">
      
        <context:annotation-config />  
        <!-- 扫描定时任务类所在的包 -->
        <context:component-scan base-package="com.test" />  
        
     </beans> 

    XML方式:

    1.定时任务类:相比注解方式,方法上面没有注解,通过XML方式注入

    @Component("mytask")
    public class MyTask {
        int i =0;
        public void doTask(){
            System.out.println("结果是:"+(i++)+":"+new Date());
        }
    }

    2.配置文件:schedule.x

    <?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:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" xmlns:aop="http://www.springframework.org/schema/aop"> <!-- <context:annotation-config /> --> <!-- 扫描定时任务类所在的包 --> <task:scheduled-tasks> <task:scheduled ref="cancelSysSeatByTask" method="doTask" cron="0 0/1 * * * ?"/>
       </task:scheduled-tasks>

    </beans>

    3.部署到tomcat并且启动,看看后台是否输出以下信息:

    结果是:0:Mon Mar 30 16:23:00 CST 2015
    结果是:1:Mon Mar 30 16:24:00 CST 2015
    结果是:2:Mon Mar 30 16:25:00 CST 2015

  • 相关阅读:
    mysql笔记05 优化服务器设置
    mysql笔记04 MySQL高级特性
    mysql笔记03 查询性能优化
    MVC模型
    mysql笔记02 创建高性能的索引
    实现服务端功能之——Java实体对象
    leetcode解题: First Unique Character in a String (387)
    vim的使用心得
    Java多线程(五)--volatile详解
    七 生成式深度学习
  • 原文地址:https://www.cnblogs.com/aisam/p/4378413.html
Copyright © 2020-2023  润新知