*******************************************
web.xml
---------------------------------------------------------------------
<listener>
<listener-class>com.sehz.listener.TimeListener</listener-class>
</listener>
*******************************************
com.sehz.listener.TimeListener
---------------------------------------------------------------------
package com.sehz.listener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TimeListener implements ServletContextListener {
private Timer timer = null;
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
timer.cancel();
}
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d1 = format.parse("2010/12/02 14:40:00");
//每天 1:00
GregorianCalendar nowJCQ = new GregorianCalendar();
// nowJCQ.set(Calendar.HOUR_OF_DAY, 1);
// nowJCQ.set(Calendar.MINUTE, 0);
// nowJCQ.set(Calendar.SECOND, 0);
timer = new Timer(true);
// timer.scheduleAtFixedRate
// -->如果指定开始执行的时间在当前系统运行时间之前,scheduleAtFixedRate会把已经过去的时间也作为周期执行,而schedule不会把过去的时间算上
timer.schedule(new SendMailTimerTask(),nowJCQ.getTime(),1*60*1000);
}catch (ParseException pe){
pe.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
/*
* Author: ChuZhenbin
* CreateTime: 2010-06-15
* Description: 检查设定的开始时间,如果开始时间小于当前时间(weblogic启动时间),则设定的日期加一, 此目的是为了避免weblogic一启动就执行一次
* 此方法只对日别执行的定时任务有效
*/
private GregorianCalendar checkFirstTime(GregorianCalendar firstTime) throws Exception {
GregorianCalendar setTime = firstTime;
GregorianCalendar now = new GregorianCalendar();
System.out.println("现在时间:"+now.getTime());
System.out.println("设定时间:"+setTime.getTime());
System.out.println("设定时间 < 现在时间:"+(setTime.getTime()).before((now.getTime())));
//如果设定时间比当前时间早,则设定时间日期加1
try{
if((setTime.getTime()).before((now.getTime()))){
setTime.set(setTime.get(Calendar.YEAR),
setTime.get(Calendar.MONTH),
setTime.get(Calendar.DAY_OF_MONTH)+1,
setTime.get(Calendar.HOUR),
setTime.get(Calendar.MINUTE),
setTime.get(Calendar.SECOND));
System.out.println("调整后的设定时间:"+setTime.getTime());
}
}catch(Exception e){
}
return setTime;
}
}
*******************************************
com.sehz.listener.SendMailTimerTask
---------------------------------------------------------------------
package com.sehz.listener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import com.sehz.communication.MySingleCommunication;
import com.sehz.wpg.base.BaseAction;
public class SendMailTimerTask extends TimerTask {
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("---------->JOB:Send Mail start at:" + format.format(new Date()));
//0.变量定义
String subject = "";
String mailbody = null;
boolean isHtml = false;
String msgtype = "official";
String local = "zh_CN";
String fromMailAddr = "";
String[] toMailAddrs = null;
String[] ccAddrs = null;
String[] bccAddrs = null;
//private
MySingleCommunication comm = new MySingleCommunication();
Map param = null;
String messageJobLog = "";
//1.从待发送邮件信息表中,取待发送的邮件
BaseAction dbClient = new BaseAction();
try{
List maillist = dbClient.getEncodedList("jobmail.getWaitingMailList", null);
//List maillist = dbClient.getList("jobmail.getWaitingMailList", null);
if(maillist == null || maillist.size() == 0){
System.out.println("There is no mail for send.");
} else {
//循环邮件列表,逐个发送
for(int i = 0; i < maillist.size(); i++){
Map mailinfo = (Map) maillist.get(i);
subject = (String) mailinfo.get("SUBJECT");
mailbody = (String) mailinfo.get("BODY");
isHtml = (mailinfo.get("ISHTML") != null && (mailinfo.get("ISHTML").toString().equals("1"))) ? true : false;
msgtype = (String) mailinfo.get("MSG_TYPE");
local = (String) mailinfo.get("LOCAL");
fromMailAddr = (String) mailinfo.get("FROM_MAIL_ADDR");
toMailAddrs = mailinfo.get("TO_MAIL_ADDRS") == null ? null : ((String) mailinfo.get("TO_MAIL_ADDRS")).split(";");
ccAddrs = mailinfo.get("CC_MAIL_ADDRS") == null ? null : ((String) mailinfo.get("CC_MAIL_ADDRS")).split(";");
bccAddrs = mailinfo.get("BCC_MAIL_ADDRS") == null ? null : ((String) mailinfo.get("BCC_MAIL_ADDRS")).split(";");
//2.调用mysingle的webservice发送邮件
String[] results = comm.sendMISMail(subject, mailbody, isHtml, msgtype, local, fromMailAddr, toMailAddrs, ccAddrs, bccAddrs);
//results[0]:mysingle返回的邮件Id
//results[1]:message
//3.发送邮件结果更新到邮件信息表中
int status = 9;//失败
if(results[0] != null && results[0].trim().length() > 0){
status = 1;//成功
}
String message = results[1];
if(message.length() > 1000){
message = message.substring(0, 1000);
}
param = new HashMap();
param.put("STATUS", new Integer(status));
param.put("MAIL_ID_FROM_SINGLE", results[0]);
param.put("MESSAGE", message);
param.put("SEQ", new Integer(mailinfo.get("SEQ").toString()));
dbClient.updateEncoded("jobmail.updateSendMailStatus", param, true);
//dbClient.update("jobmail.updateSendMailStatus", param, true);
message = results[1];
if(message.length() > 2000){
message = message.substring(0, 2000);
}
//4.发送邮件结果存入log表
//createJobLog
param = new HashMap();
param.put("JOB_TABLE_NAME", "COMM_MASTER_MAIL");
param.put("PK_FIRST", mailinfo.get("SEQ"));
param.put("PK_SECOND", null);
param.put("PK_THIRD", null);
param.put("JOB_MESSAGE", message);
dbClient.updateEncoded("jobmail.createJobLog", param, true);
//dbClient.update("jobmail.createJobLog", param, true);
}
}
}catch(Exception se){
//se.printStackTrace();
messageJobLog += se.toString() + " /n ";
StackTraceElement[] stes = se.getStackTrace();
for(int i = 0; i < stes.length; i++){
messageJobLog += stes[i].toString() + " /n ";
}
// System.out.println("message-->");
// System.out.println(messageJobLog);
// System.out.println("printStackTrace-->");
se.printStackTrace();
if(messageJobLog.length() > 2000){
messageJobLog = messageJobLog.substring(0, 2000);
}
param = new HashMap();
param.put("JOB_TABLE_NAME", "COMM_MASTER_MAIL");
param.put("PK_FIRST", null);
param.put("PK_SECOND", null);
param.put("PK_THIRD", null);
param.put("JOB_MESSAGE", messageJobLog);
try {
dbClient.updateEncoded("jobmail.createJobLog", param, true);
//dbClient.update("jobmail.createJobLog", param, true);
} catch (SQLException se1) {
se1.printStackTrace();
}
}
//System.out.println(" mail sended..result is:" + result);
System.out.println("<---------JOB:Send Mail end.[" + format.format(new Date()) + "]");
}
}
*******************************************