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

  • 相关阅读:
    POJ数据结构专辑(含部分题解)
    第K小数 uva 10041 Vito's Family poj 2388 Who's in the Middle
    POJ 1195 Mobile phones (二维树状树组)
    python 学习体会
    ACM竞赛常用STL(二)之STLalgorithm
    计算机科学中的树
    ctf古典密码从0到
    漏洞挖掘的艺术面向源码的静态漏洞挖掘
    漏洞挖掘的艺术面向二进制的静态漏洞挖掘
    实战演示 H5 性能分析
  • 原文地址:https://www.cnblogs.com/aisam/p/4378413.html
Copyright © 2011-2022 走看看