`

Java 定时器 定点执行

    博客分类:
  • java
 
阅读更多
web.xml里监听
<listener>
     <listener-class>com.tzx.common.util.ftpscan.TimerRun</listener-class>
</listener>
package com.tzx.common.util.ftpscan;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;





public class TimerRun implements ServletContextListener {
	private static Log log = LogFactory.getLog(TimerRun.class);
	private ScheduledExecutorService executorService = Executors
			.newScheduledThreadPool(1);

	public void contextDestroyed(ServletContextEvent arg0) {
		executorService.shutdown();

	}

	public void contextInitialized(ServletContextEvent arg0) {
		log.info("------定时--execute---ftpService--start------");
		run();
		log.info("------定时--execute---ftpService--end------");
	}

	/**
	 * 定时执行 每天凌晨定时执行任务
	 */
	private void run() {
		long oneDay = 24 * 60 * 60 * 1000;
		long initDelay = getTimeMillis("23:59:59") - System.currentTimeMillis();
		initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;

		executorService.scheduleAtFixedRate(new Runnable() {
			public void run() {
				try {
					FTPListAllFiles.ftpService();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}, initDelay, oneDay, TimeUnit.MILLISECONDS);
		
	}

	/**
	 * 获取指定时间对应的毫秒数
	 * 
	 * @param time
	 *            "HH:mm:ss"
	 * @return
	 */
	private static long getTimeMillis(String time) {
		try {
			DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
			DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
			Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " "
					+ time);
			return curDate.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
}

分享到:
评论
1 楼 u011314252 2014-12-25  
紫龙霸气啊啊啊啊

相关推荐

    java每天实现定点执行任务java每天实现定点执行任务

    本文将详细探讨如何在Java中实现每天定点执行任务,主要涉及的核心知识点包括Java的定时框架如ScheduledExecutorService和Quartz Scheduler,以及如何配置任务定时器。 首先,`ScheduledExecutorService` 是Java...

    Android中定时执行任务的3种实现方法(推荐)

    这种方法是基于Java的线程机制,通过`Thread.sleep()`方法让线程休眠指定时间,然后发送消息到主线程的Handler来执行任务。虽然简单,但存在以下问题: 1. 不精确:`Thread.sleep()`可能会因为系统调度等原因导致...

    TaskAlert 任务提醒

    通常,开发者会使用编程语言中的定时器API(如Java的ScheduledExecutorService,Python的schedule库等)来实现定时任务,结合事件驱动编程,当时间到达设定点时,触发提醒功能。 2. **程序启动与自启动选项**:用户...

    辩论赛计时器

    如果是源代码文件,可能的扩展名有.cpp、.java、.py等,对应C++、Java或Python等编程语言。如果是资源文件夹,可能包含图片、音频文件、配置文件等,这些都是构建完整计时器应用所必需的组成部分。 开发辩论赛计时...

    EventTest.zip

    1. **事件驱动编程**:这是一种编程范式,其中,程序的执行由外部事件(如用户输入、系统消息、定时器等)驱动,而不是连续的指令序列。 2. **事件监听器/处理器**:这是事件驱动编程中的核心组件,负责注册对特定...

    TMS320DM642中文手册

    ### TMS320DM642视频/图像定点数字信号处理器 #### 1.1 特点 - **高性能数字媒体处理器**:TMS320DM642是一款高性能数字媒体处理器,具备2-、1.67-、1.39-纳秒的指令周期,支持500-、600-、720-MHz的时钟频率,...

    Android 安卓手机定时发送短信 ,多条短信同时发布-IT计算机-毕业设计.zip

    在定时发送短信的场景下,开发者会使用AlarmManager设置一个定时器,当时间到达设定点时,触发发送短信的意图。 除了核心的编程概念,这个项目还涉及到多线程处理,因为多条短信需要同时发布。在Android中,网络...

    JBPM5 用户手册 (英文,PDF,有书签)

    - **4.1.3 事件**:事件机制使得开发人员能够监听流程中的特定点,并执行相应的操作。 **4.2 基于知识的API** 这部分深入介绍了如何使用Knowledge Base API来管理和操作流程定义。 #### 五、核心引擎:基础知识 ...

Global site tag (gtag.js) - Google Analytics