登录  /  注册
首页 > Java > java教程 > 正文

Spring整合TimerTask实现定时任务调度

高洛峰
发布: 2017-02-07 15:27:35
原创
1442人浏览过

一. 前言

最近在公司的项目中用到了定时任务, 本篇博文将会对TimerTask定时任务进行总结, 其实TimerTask在实际项目中用的不多, 
因为它不能在指定时间运行, 只能让程序按照某一个频度运行.

二. TimerTask

JDK中Timer是一个定时器类, 它可以为指定的定时任务进行配置.
JDK中TimerTask是一个定时任务类, 该类实现了Runnable接口, 是一个抽象类, 我们可以继承这个类, 实现定时任务.

/** 
 * 继承TimerTask实现定时任务 
 */
public class MyTask extends TimerTask { 
  
  @Override
  public void run() { 
    String currentTime = new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(new Date()); 
    System.out.println(currentTime + " 定时任务正在执行..."); 
  } 
  
  public static void main(String[] args) { 
    Timer timer = new Timer(); 
      
    // 1秒钟执行一次的任务, 参数为: task, delay, peroid 
    timer.schedule(new MyTask(), 2000, 1000); 
  } 
}
登录后复制

三. 整合Spring

两个核心类: ScheduledTimerTask, TimerFactoryBean
ScheduledTimerTask类是对TimerTask的包装器实现, 通过该类可以为这个任务定义触发器信息.
TimerFactoryBean类可以让Spring使用配置创建触发器, 并为一组指定的ScheduledTimerTask bean自动创建Timer实例.

1. 引入Jar包: spring.jar, commons-logging.jar
2. 定时调度业务类:

/** 
 * 定时调度业务类 
 */
public class TaskService extends TimerTask { 
  private int count = 1; 
  
  public void run() { 
    System.out.println("第" + count + "次执行定时任务"); 
    count++; 
  } 
}
登录后复制

3. 核心配置:

<?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.5.xsd"> 
    
  <bean id="taskService" class="com.zdp.service.TaskService"></bean> 
  <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> 
    <property name="timerTask" ref="taskService" /> 
      
    <!-- 每隔一天执行一次配置: 24*60*60*1000 -->
    <!-- 每1秒钟程序执行一次 -->
    <property name="period" value="1000" /> 
      
    <!-- 程序启动4秒钟后开始执行 -->
    <property name="delay" value="4000" /> 
  </bean> 
    
  <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> 
    <property name="scheduledTimerTasks"> 
      <list> 
        <ref bean="scheduledTimerTask" /> 
      </list> 
    </property> 
  </bean> 
</beans>
登录后复制

4. 测试类:

public class Main { 
  public static void main(String[] args) { 
    // 加载spring配置文件 
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    System.out.println("<<-------- 启动定时任务 -------- >>"); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    while (true) { 
      try { 
        if (reader.readLine().equals("quit")) { 
          System.out.println("<<-------- 退出定时任务 -------- >>"); 
          System.exit(0); 
        } 
      } catch (IOException e) { 
        throw new RuntimeException("error happens...", e); 
      } 
    } 
  } 
}
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多Spring整合TimerTask实现定时任务调度相关文章请关注PHP中文网!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号