web.xml 配置定時器 詳解

1.首先在web.xml中配置,監聽器

<!-- 定時器監聽 -->
	<listener>
		<listener-class>com.checkoo.ok.excl.TaskManager</listener-class>
	</listener>

2.監聽器類

import java.util.Calendar;
import java.util.Timer;

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

import org.apache.commons.lang.time.DateUtils;

public class TaskManager implements ServletContextListener {

	//每天的毫秒數
	public static final long DAY = 86400000;
	//定時器
	private Timer timer;
	
	/**
	 * 在Web應用結束時停止任務
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		timer.cancel();//定時器銷燬

	}

	/**
	 * 在Web應用啓動時初始化任務
	 */
	public void contextInitialized(ServletContextEvent sce) {
		//定義定時器
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, 1);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		timer = new Timer(true);
		LocationTask locationTask = new LocationTask();//定時執行的內容
		locationTask.setWt(sce.getServletContext().getRealPath(""));
		//timer.schedule(locationTask, c.getTime(), DAY); //定時器在每日凌晨0點執行
		timer.schedule(locationTask, 5000, 3600000); // 啓動後5秒執行,後每隔1小時在執行
	}
}

3.內容類(我這裏就以導出excl文件爲例)

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class LocationTask extends TimerTask {
	private String wt;
	private static boolean isRunning = false;

	@Override
	public void run() {
		if (!isRunning) {
			isRunning = true;
			System.out.println("執行excle導出");
			excelPage();
			isRunning = false;
		} else {
			System.out.println("執行錯誤");
		}

	}

	@SuppressWarnings({ "deprecation" })
	public void excelPage() {
		List datas = DB查詢;
		try {
			//第一步,創建一個webbook,對應一個Excel文件
	        HSSFWorkbook wb = new HSSFWorkbook();
	        //第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
	        HSSFSheet sheet = wb.createSheet("sheet名字");
	        //第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
	        HSSFRow row = sheet.createRow((int)0);
	        //第四步,創建單元格,並設置值表頭  設置表頭居中
	        HSSFCellStyle style = wb.createCellStyle();
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //創建一個居中格式
	        
	        HSSFCell cell = row.createCell((short)0);
	        cell.setCellValue("用戶名"); cell.setCellStyle(style);
	        cell = row.createCell((short)1);
	        cell.setCellValue("密碼"); cell.setCellStyle(style);
	        cell = row.createCell((short)2);
	        
	        

	        for(int i=0;i<datas.size();i++){
	            row = sheet.createRow((int)i+1);
	            Data data = datas.get(i);
	            //第四步,創建單元格,並設置值
	            row.createCell((short)0).setCellValue(data.get用戶名);
	            row.createCell((short)1).setCellValue(data.get密碼);
	        }

	File file = new File(wt + "/路徑");
	if (!file.exists()) {
		file.mkdirs();
	}
	FileOutputStream fout = new FileOutputStream(wt + "/路徑/" + "文件名" +".xls");
            wb.write(fout);
            fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public String getWt() {  //服務器路徑
		return wt;
	}

	public void setWt(String wt) {
		this.wt = wt;
	}

}

ok,寫完收工~~~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章