zoukankan      html  css  js  c++  java
  • Spring 定时器

    spring定时器写在web工程中

    配置文件的名字如上

    配置文件的内容下面:

    <?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:task="http://www.springframework.org/schema/task" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-3.1.xsd">
    
        <task:annotation-driven/> 
         <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 -->  
        <task:scheduled-tasks scheduler="myScheduler">  
            <!-- 规定运行时间 -->
            <task:scheduled ref="TestJob" method="execute" cron="1/5 * * * * *"/>
            <!-- 按间隔时间 -->
            <task:scheduled ref="TestJob" method="execute" fixed-rate="50000" fixed-delay="300"/>
        </task:scheduled-tasks>  
        
        <task:scheduler id="myScheduler" pool-size="10"/>  
    </beans>

    配置文件说明:

    ref是工作类

    method是工作类中要执行的方法

    initial-delay是任务第一次被调用前的延时,单位毫秒

    fixed-delay是上一个调用完成后再次调用的延时

    fixed-rate是上一个调用开始后再次调用的延时(不用等待上一次调用完成)

    cron是表达式,表示在什么时候进行任务调度。

    测试类:

    package com.um.job;
    import org.springframework.stereotype.Component;
    //指定定时器名称
    @Component("TestJob")  
    public class TestJob {
        private static int counter = 0;
        public void execute() {
            System.out.println("定时器开始执行");
        }
    }

     将Job.xml引入spring配置文件

    <!-- 模块化处理spring定时任务 -->
    <import resource="classpath:config/spring-context-job.xml"></import>

  • 相关阅读:
    arc模式和ios的关系
    uitableview置底部,不显示到最顶层
    Mac OS X Lion 10.7.4 升级包
    ObjectiveC urlencode/urldecode url加密解密
    UITableView阴影
    iOS SDK: Working with URL Schemes
    黑苹果mac lion 10.7.3升级10.7.4
    查找 EXC_BAD_ACCESS 问题根源的方法
    "unrecognized selector sent to instance"问题的解决
    测量应用程序cass和cad的使用感受
  • 原文地址:https://www.cnblogs.com/guokai870510826/p/5827145.html
Copyright © 2011-2022 走看看