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配置文件 WebRoot\WEB-INF\applicationContext.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文件配置

   同上...

 

Spring任务调度使用,布布扣,bubuko.com

Spring任务调度使用

上一篇:C语言中静态库和动态库笔记


下一篇:CXF spring配置引用标签