www.pudn.com > Javamail.rar > MyTask.java


package com.digipower.automail.mailsender;

import javax.servlet.*;
import java.util.*;
import com.digipower.automail.AutoMailConstants;
import java.io.*;
import org.apache.log4j.*;
/**
 * 

Title:

*

Description: TestStruts

*

Copyright: Copyright (c) 2004

*

Company: foxconn

* @author conti * @version 1.0 */ public class MyTask extends TimerTask { public MyTask() { } private static final int C_SCHEDULE_HOUR = AutoMailConstants.SCHEDULE_HOUR; private static boolean isRunning = false; private ServletContext context = null; private static Logger s_log = null; private static Properties s_prop = new Properties(); String strFromAddress = null; String strReplyAddress = null; String strReplyToAddress = null; public MyTask(ServletContext context) { this.context = context; s_log = Logger.getLogger(MyTask.class); PropertyConfigurator.configure(context.getRealPath("/") + "\\WEB-INF\\log4j.inf"); } public void run() { Calendar cal = Calendar.getInstance(); try { if (!isRunning) { FileInputStream fis = null; try { fis = new FileInputStream(context.getRealPath("/") + "\\WEB-INF\\automail.properties"); s_prop.load(fis); fis.close(); } catch (FileNotFoundException e) { s_log.warn(cal.getTime() + " " + "automail.properties does not exists."); } int iTime = 0; String str = s_prop.getProperty("SendTime"); strFromAddress = s_prop.getProperty("FromAddress"); strReplyAddress = s_prop.getProperty("ReplyAddress"); strReplyToAddress = s_prop.getProperty("ReplyToAddress"); if(str != null) { iTime = Integer.parseInt(str); } System.out.println("iTime:" + iTime); s_log.warn(cal.getTime() + " " + context.getRealPath("/") + "\\WEB-INF\\automail.properties"); s_log.warn(cal.getTime() + " iTime: " + String.valueOf(iTime)); s_log.warn(cal.getTime() + " " + "FromAddress:" + strFromAddress); s_log.warn(cal.getTime() + " " + "ReplyAddress:" + strReplyAddress); // s_log.warn(cal.getTime() + " " + "Manager:" + AutoSend.MANAGER_ADDRESS.length); if (iTime == cal.get(Calendar.HOUR_OF_DAY)) { isRunning = true; //TODO 添加自定义的详细任务,以下只是示例 System.out.println("The Listener Running"); System.out.println(cal.get(Calendar.HOUR_OF_DAY)); // s_log.warn(cal.getTime() + " " + context.getRealPath("/") + "\\WEB-INF\\automail.properties"); // s_log.warn(cal.getTime() + " " + String.valueOf(iTime)); //send mail action AutoSend send = new AutoSend(); send.autoSend(strFromAddress, strReplyAddress, strReplyToAddress); isRunning = false; } else { System.out.println("C_SCHEDULE_HOUR:" + C_SCHEDULE_HOUR); System.out.println("No Sending"); System.out.println(cal.get(Calendar.HOUR_OF_DAY)); } } else { System.out.println("No Running"); } } catch(Exception e) { System.out.println(e); } } }