`
thinktothings
  • 浏览: 766735 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring2.0 job 定时任务

阅读更多
package com.tht.common.job.spring;

import java.util.TimerTask;
import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: liuwen
 * Date: 2010-11-6
 * Time: 19:34:49
 * To change this template use File | Settings | File Templates.
 * 需要执行的任务
 */
public class DemoTask extends TimerTask{
    Logger log=Logger.getLogger(DemoTask.class);

    /**
     * 需要执行的任务写在这里
     */
    @Override
    public void run() {
        log.info("run");
    }
}

 

beans-config.xml  放到src目录下

 

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  
    <bean id="demoTask" class="com.tht.common.job.spring.DemoTask"/><!-- 时钟任务,执行任务时,会调用该类中的 run()方法,来执行。。。 -->
    
    <bean id="scheduledTimerTask" 
          class="org.springframework.scheduling.timer.ScheduledTimerTask">
        <property name="timerTask" ref="demoTask"/>
        <property name="period" value="5000"/><!--5000毫秒 即每隔5秒执行定时任务   -->
        <property name="delay" value="1000"/><!--1000毫秒 即1秒后 执行定时任务   -->
    </bean>
    
    <bean id="timerFactoryBean" 
          class="org.springframework.scheduling.timer.TimerFactoryBean">
        <property name="scheduledTimerTasks">
            <list>
                <ref bean="scheduledTimerTask"/>
            </list>
        </property>
    </bean>
    
</beans>

 

package com.tht.common.job.spring;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;

/**
 * Created by IntelliJ IDEA.
 * User: liuwen
 * Date: 2010-11-6
 * Time: 19:43:29
 * To change this template use File | Settings | File Templates.
 * 启动类,并控制何时关闭时钟任务
 */
public class TimerTaskDemo{
     static Logger log=Logger.getLogger(DemoTask.class);


    public static void main(String[] args){
        ApplicationContext context=new ClassPathXmlApplicationContext("beans-config.xml");
        log.info("启动任务。。。。。。");
        log.info("请输入exit,关闭任务");
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        while(true){
            try {
                if(reader!=null && "exit".equals(reader.readLine())){
                    break;
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e.fillInStackTrace());
            }
        }

        Timer timer =(Timer)context.getBean("timerFactoryBean");
        timer.cancel();
    }
}
 
分享到:
评论

相关推荐

    spring2.0技术手册

    林信良著 spring2.0技术手册 《Spring 2.0技术手册》介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与...

    spring2.0 Quartz 执行每天定时任务 普通普是执行任务

    NULL 博文链接:https://thinktothings.iteye.com/blog/804738

    spring2.0学习笔记+spring定时任务

    NULL 博文链接:https://tw5566.iteye.com/blog/423883

    Spring 2.0 spring 2.0 标准API

    Spring 2.0 标准API 用处不大的资源我不发

    Spring2.0jar包

    Spring2.0jar包,J2EE项目中,用于集成的框架。

    Spring 2.0 中文参考手册

    Spring 2.0 中文参考手册,Spring 2.0 中文参考手册,Spring 2.0 中文参考手册,Spring 2.0 中文参考手册Spring 2.0 中文参考手册,

    spring2.0 使用 Quartz 必备包

    spring2.0 使用 Quartz 必备包 里面有: quartz-all-1.5.2.jar spring-support.jar 适合:SSH框架 spring2.0 strut2.0 hibernate3.2等

    详尽的Spring2.0学习提纲

    对于学习Spring2.0很好的指导作用,给初学者指明一个学习的方向和思路。

    spring2.0jar包(一)

    spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一)

    Spring2.0 job 普通类中定时任务

    NULL 博文链接:https://thinktothings.iteye.com/blog/804695

    Spring2.0技术手册(林信良)_part2

    本书介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者...

    spring2.0 中文帮助文档 pdf

    spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档

    SPRING2.0开发详解

    SPRING2.0开发详解: 详细阐述了SPRING2.0的核心技术和框架整合能力

    Spring2.0技术手册

    本书介绍了Spring 2.0的新特性,诸如Spring 2.0的新配置、新AOP支持、增强的IoC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者...

    Spring2.0中文教程

    Spring2.0中文教程

    spring2.0技术手册 (扫描版)

    第1章认识Spring 第2章Spring入门 第3章Bean、消息、事件 第4章SpringAOP 第5章JDBC、事务支持 第6章Hibernate与Spring 第7章SpringWebMVC框架 第8章View层方案、Web框架整合 第9章API封装 第10章项目:...

    spring2.0中文手册及使用指南 chm

    spring2.0中文手册及使用指南,chm格式

    spring2.0中文文档

    spring2.0中文文档

    Spring2.0宝典源代码

    Spring2.0宝典全书源代码,作者李刚,随书所附的光盘

    spring2.0(中文)

    本书介绍了Spring2.0的新特性,诸如Spring2.0的新配置、新AOP支持、增强的IOC、JDBC和form标签等新功能。它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者摸索...

Global site tag (gtag.js) - Google Analytics