zoukankan      html  css  js  c++  java
  • Spring任务调度使用

    使用方法:

    基于XML方式配置调用:

    1 任务类实现:

    package com.demo.schedule;
    
    public class ScheduleTask {
        private int i=0;
        public void process(){
            System.out.println("run to " + i++ + " times");
        }
    }

    2 spring配置文件 WebRootWEB-INFapplicationContext.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:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
            
        <!-- 任务bean -->
        <bean id="scheduleTask" class="com.demo.schedule.ScheduleTask"></bean>
        <task:scheduler id="scheduler" pool-size="8"/>
        <task:scheduled-tasks scheduler="scheduler">
            <!-- 定时调用,间隔5s执行一次 -->
             <task:scheduled ref="scheduleTask" method="process" fixed-delay="5000"/>              
         </task:scheduled-tasks>    
    </beans>

    3 web.xml文件配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>

    将项目部署到tomcat服务器上即可定时运行,运行结果:

    ......

    run to 112 times
    run to 113 times
    run to 114 times

    基于Annotation方式配置调用:

    package com.demo.schedule;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component("scheduleTask")
    public class ScheduleTask {
        private int i = 0;
    
        @Scheduled(fixedDelay = 5000)
        public void process() {
            System.out.println("run to " + i++ + " times");
        }
    }

    spring配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
        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-2.5.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:annotation-config /> 
        <context:component-scan base-package="com.demo.schedule" />  
        <task:annotation-driven />      
    
        <!-- 任务bean 
        <bean id="scheduleTask" class="com.demo.schedule.ScheduleTask"></bean>
        -->
        <task:scheduler id="scheduler" pool-size="8"/>
        <task:scheduled-tasks scheduler="scheduler">
            <!-- 定时调用,间隔5s执行一次 -->
             <task:scheduled ref="scheduleTask" method="process" fixed-delay="5000"/>              
         </task:scheduled-tasks>    
    </beans>

    3 web.xml文件配置

       同上...

  • 相关阅读:
    从零打造树莓派智能助手(一)——让树莓派说话
    以np.concatenate为主题,谈谈numpy数组按维度合并的问题
    树莓派apt报错:E: 'Release' 这个值对 APT::Default-Release 是无效的,因为在源里找不到这样的发行
    我遇到的一些Git问题汇编
    在Mac平台用Sublime编辑器使用Git并连接github
    LeetCode108——Convert Sorted Array to Binary Search Tree
    LeetCode122——Best Time to Buy and Sell Stock II
    LeetCode686——Repeated String Match
    Python爬虫 — 百度翻译
    HTTP 协议
  • 原文地址:https://www.cnblogs.com/hutton/p/3813361.html
Copyright © 2011-2022 走看看