• 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

  • 相关阅读:
    excel 读取
    MSDN异步编程概述 [C#] zzhttp://www.cnblogs.com/hxhbluestar/articles/60023.html
    window.opener showModelessDialog showModalDialog 获取|控制父窗体的区别
    MySql中文乱码解决方法
    关于随机数
    javascript 日期处理(注意事项)
    一个简单访问office程序的控件,不依赖officedll
    关于12306的bug
    回车提交
    js动态添加外部js(顶)
  • 原文地址:https://www.cnblogs.com/aisam/p/4378413.html
Copyright © 2020-2023  润新知