码迷,mamicode.com
首页 > 编程语言 > 详细

Spring任务调度使用

时间:2014-07-07 19:36:28      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   使用   

使用方法:

基于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任务调度使用

标签:style   blog   http   java   color   使用   

原文地址:http://www.cnblogs.com/hutton/p/3813361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!