`
jxqc_job
  • 浏览: 529 次
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
java web定时任务 java web定时任务
需要导入struts的commons-io-2.0.1.jar包
java web 定时任务的实现:
一. 自定义一个监听器
1. MyListener.java
package com.tdtech.pdm.lisener;

import java.util.Timer;

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

import com.tdtech.pdm.util.TaskExecution;
/**
 * 自定义监听器
 */
public class MyLisener implements ServletContextListener{
	private Timer t;
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		if(t != null){
			t.cancel();
		}
		System.out.println("监听器销毁....");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		t = new Timer();
		t.schedule(new TaskExecution(), 10 * 1000, 24 * 60 * 60 * 1000);
	}

}

二.定义一个定时任务类
2. TaskExecution.java
package com.tdtech.pdm.util;

import java.util.TimerTask;
/**
 * 定时任务
 */
public class TaskExecution extends TimerTask{
	@Override
	public void run() {
		System.out.println("定时任务启动了....");
		FileUtil.main(null);
	}
}

三.定义一个文件备份的工具类
3. FileUtil.java
package com.tdtech.pdm.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
/**
 * 文件拷贝
 */
public class FileUtil {
	private String fileName = "D:\\codefile\\testfile\\";
	private String destDirectory = "D:\\test\\backup\\";
	private void copyFile() throws IOException{
		File file = new File(fileName); 
		if(file.exists()){
			if(file.isDirectory()){
				File[] fs = file.listFiles();
				//全部复制
				for(File f : fs){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					String prefix = f.getName().substring(f.getName().lastIndexOf("."));
					String fname = f.getName().substring(0, f.getName().lastIndexOf("."))+"_";
					File df = new File(destDirectory + fname + sdf.format(new Date()) + prefix);
					if(!df.exists()){
						df.createNewFile();
					}else{
						df.delete();
						df.createNewFile();
					}
					FileUtils.copyFile(f, df);
				}
			}
		}
	}
	public static void main(String[] args) {
		FileUtil fu = new FileUtil();
		try {
			fu.copyFile();
			System.out.println("文件备份成功.....");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

四.在web.xml中配置自定义监听器
4. <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <listener>
  	<display-name>配置自定义监听器</display-name>
  	<listener-class>com.tdtech.pdm.lisener.MyLisener</listener-class>
  </listener>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
Global site tag (gtag.js) - Google Analytics