zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ffmpeg使用中文使用+解释
    CentOS GCC升级到4.6.1(转)
    C#委托与直接调用方法区别
    微软面试题,倒置数组
    MSDN中介绍的图片格式
    你们的学校是几流
    VS代码模版
    正则表达式
    Visual Studio 2010中添加项目模板
    泛型详解
  • 原文地址:https://www.cnblogs.com/aisam/p/4378413.html
Copyright © 2011-2022 走看看