欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 监听器

程序员文章站 2022-05-16 22:05:46
...
java 监听器

www.firnow.com 时间 : 2009-10-20 作者:匿名 编辑:小张 点击: 314 [ 评论 ]

1、首先配置web.xml文件

<listener>
<listener-class>cn.com.spcs.ots.common.util.OverTimeSystemListener</listener-class>
</listener>

2、实现ServletContextListener接口

contextInitialized(final ServletContextEvent arg0)

contextDestroyed(ServletContextEvent arg0)



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import cn.com.cs.ots.bean.dao.DB2DAOImpl;
import cn.com.cs.ots.bean.dao.IDAO;
import cn.com.cs.ots.common.Constant;
/**
* 加班休假系统批处理启动
*
* @author
*/
public class OverTimeSystemListener implements ServletContextListener {
private Timer timer = null;
private TimerTask task = null;
protected IDAO dao;
private int executeCount = 0;
// 月份变量
private static int month = 1;
// 日期变量
private static int day = 1;
// 分钟变量
private static int minute = 1;
// 小时变量
private static int hour = 1;
// 秒变量
private static int second = 1;
/**
* 加班休假系统监听方法contextInitialized
*
* @param arg0
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
public void contextInitialized(final ServletContextEvent arg0) {
SysLogger.info(this, "contextInitialized is start.");
timer = new Timer();
task = new TimerTask() {
public void run() {
try {
GregorianCalendar date = new GregorianCalendar();
month = date.get(Calendar.MONTH);
day = date.get(Calendar.DATE);
hour = date.get(Calendar.HOUR_OF_DAY);
minute = date.get(Calendar.MINUTE);
second = date.get(Calendar.SECOND);
// 日批处理类
EntryExitFileListener entryExitFileListener = new EntryExitFileListener();
// 月批处理类
AttendanceRecordListener attendanceRecordListener = new AttendanceRecordListener();
YearInput yearInput = new YearInput();
if ((hour == 15) && (minute == 35) && (second == 00)) {
// 初始化Dao
initDao(arg0);
getConnection();
String start = "20081101";
String end = "20081108";
while ((end.length()) == 8) {
if (start.equals(end)) {
break;
}
entryExitFileListener.doDayBatch(dao, start,
getNextDay(start));
start = getNextDay(start);
}
closeConnection();
}
// 每日6:00调用日批处理中方法,处理下班打卡记录、门卡信息和加班记录
if ((hour == 9) && (minute == 30) && (second == 00)) {
// String yesterdayDate = getYesterdayDate();
// String todayDate = getNextDay(yesterdayDate);
//
// // 初始化Dao
// initDao(arg0);
// getConnection();
// entryExitFileListener.doDayBatch(dao, yesterdayDate,
// todayDate);
// closeConnection();
}
// 每月一日10:00启动线程,导入考勤记录
// if ((day == 1) && (hour == 10) && (minute == 0)
// && (second == 1)) {
//
// // 初始化Dao
// initDao(arg0);
// getConnection();
// attendanceRecordListener.doStartTimeBatchSelect(dao);
// closeConnection();
//
// }
//
// if (executeCount == 0
// || (month == 12 && day == 30 && hour == 22
// && minute == 0 && second == 0)) {
// // 初始化Dao
// initDao(arg0);
// getConnection();
// yearInput.getInitValue(dao, executeCount);
// closeConnection();
// executeCount++;
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnection();
}
}
};
timer.schedule(task, 10000, 500);
}
/**
* 监听结束方法contextDestroyed
*
* @param arg0
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
public void contextDestroyed(ServletContextEvent arg0) {
timer.cancel();
SysLogger.info(this, "contextInitialized is end.");
}
/**
* 初始化dao
*
* @param arg
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
private void initDao(ServletContextEvent arg) throws Exception {
dao = new DB2DAOImpl();
dao.setSqlTemplateName("cn.com.cs.ots.sql.SQLTemplateImpl");
dao.setSqlMap((Map) arg.getServletContext().getAttribute(
Constant.SQLTEMPLATE_KEY));
dao.setReturnTypeMap((Map) arg.getServletContext().getAttribute(
Constant.RETURNTYPE_KEY));
}
/**
* getConnection
*
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
private void getConnection() {
try {
dao.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* closeConnection
*
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
private void closeConnection() {
try {
if (dao != null)
dao.closeConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到前一天的String类型
*
*
* @param dateTime
* @return String
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
private String getYesterdayDate() {
String dateTime = null;
Date pre_date = new Date(System.currentTimeMillis() - 1 * 24 * 3600
* 1000);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
dateTime = formatter.format(pre_date);
return dateTime;
}
/**
* 得到下一天的String类型
*
*
* @param dateTime
* @return String
* @throws ParseException
*
* 备注<br>
* 作者: 作成日:2008-10-22<br>
*/
public String getNextDay(String dateTime) throws ParseException {
Calendar now = Calendar.getInstance();
SimpleDateFormat simpledate = new SimpleDateFormat("yyyyMMdd");
Date date = null;
date = simpledate.parse(dateTime);
now.setTime(date);
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH) + 1;
now.set(year, month, day);
String time = simpledate.format(now.</p>
<div align="center" class="pager"><span id="pagesSpan"> <span id="1">1</span> <a href="179650_2.html">2</a></span></div>
<div class="ad7"><script language="javascript" src="/ad/2010/article/ad7.js"></script></div>
<div class="ad8"><script language="javascript" src="/ad/2010/article/ad8.js"></script></div>
<div class="index_main_err">如果图片或页面不能正常显示请<a href="javascript:void(0)" onClick="ReportError()" class= "redlink"><font color="#990000"><strong>点击这里</strong></font></a></div>
</div>
<div class="index_main_left_foot">
<div class="index_main_left_foot1">
<p><a href="javascript:window.external.addFavorite(self.location,document.title);">【收藏此页】</a><a href="http://bbs.firnow.com" target="_blank">【飞诺社区】</a><a href="#comment">【发表评论】</a><a href="javascript:window.close()">【关闭】</a></p>
<a href="#comment"><img src="/images/2010/article/ping.gif" width="149" height="34" border="0" /></a>
</div>
<div class="index_main_left_foot2">
<p>上一篇:<a href="/course/3_program/java/javajs/20091020/179649.html">JSON所要依赖的jar包有</a></p>
<p>下一篇:<a href="/course/3_program/java/javajs/20091020/179651.html">java 中 set map table list 的总结</a></p>
</div>
</div>
</div>
<div class="ad9"><script language="javascript" src="/ad/2010/article/ad9.js"></script></div>
<div class="index_main_left_3">Java技术文章推荐文章</div>
<div class="index_main_left_4">
<ul>
<li><a href="/course/3_program/java/javajs/20090410/164705.html" target="_blank">Java继承关系、构造器的内存分析</a></li>
<li><a href="/course/3_program/java/javajs/20071023/79561.html" target="_blank">java中返回局部对象问题</a></li>
<li><a href="/course/3_program/java/javajs/2007128/91137.html" target="_blank">Windows下的OpenLDAP配置</a></li>
<li><a href="/course/3_program/java/javajs/2008525/117792.html" target="_blank">Java中如何正确使用字体编码</a></li>
<li><a href="/course/3_program/java/javajs/20100112/189415.html" target="_blank">Observer模式(转载)</a></li>
<li><a href="/course/3_program/java/javajs/20090405/164210.html" target="_blank">java与模式 多例模式 multiton</a></li>
<li><a href="/course/3_program/java/javajs/20090515/166931.html" target="_blank">java中取得微秒级的时间 - lujinan858的专栏 - CSDN博客</a></li>
<li><a href="/course/3_program/java/javajs/20091118/182766.html" target="_blank">JS实现树形菜单</a></li>
</ul>
<ul>
<li><a href="/course/3_program/java/javajs/20091220/185101.html" target="_blank">Spring读取加密属性文件处理</a></li>
<li><a href="/course/3_program/java/javajs/20071022/79309.html" target="_blank">李开复:算法的力量</a></li>
<li><a href="/course/3_program/java/javajs/20090226/156187.html" target="_blank">介绍JNDI的简单概念&简单示例</a></li>
<li><a href="/course/3_program/java/javajs/20090511/166905.html" target="_blank">JAVA写日志整理代码</a></li>
<li><a href="/course/3_program/java/javajs/20091006/178113.html" target="_blank">java抽象类和抽象方法</a></li>
<li><a href="/course/3_program/java/javajs/2007927/74820.html" target="_blank">Java学习:多线程(2)</a></li>
<li><a href="/course/3_program/java/javajs/200866/122754.html" target="_blank">struts分页</a></li>
<li><a href="/course/3_program/java/javajs/20090505/166570.html" target="_blank">进程和线程的区别</a></li>
</ul>
</div>
<div class="index_main_left_5">
<div class="index_main_left_5_top"><p><a name="comment"></a>文章评论</p></div>
<div id="divComment" class="index_main_left_5_main"></div>
</div>
<div class="index_main_left_6">
<div class="index_main_left_6_left">
<div class="index_main_left_6_left_top">请您留言</div>
<div class="index_main_left_6_left_ceneter">
<form>
<table width="84%" height="216" border="0" cellpadding="10">
<tr>
<td width="19%" height="29">昵称:</td>
<td width="81%"><label>
<input type="text" class="index_main_left_6_left_ceneter_text" style="width:150px;" name="tbName" id="tbName" onclick="this.focus();this.select()" maxlength="20" />
</label></td>
</tr>
<tr>
<td>验证码:</td>
<td>
<span style="float:left;"><input name="tbCode" id="tbCode" class="index_main_left_6_left_ceneter_text" type="text" size="6" /></span><span id="spanCode" style="float:left; clear:right;"><img id="Img2" onclick="this.src=this.src" style="display:none;"/></span>
</tr>
<tr>
<td height="26" colspan="2"><a href="http://bbs.firnow.com/register.aspx" style="color:#FF0000">注册会员</a> <a href="http://bbs.firnow.com/login.aspx">会员登录</a></td>
</tr>
<tr>
<td height="100" colspan="2"><label>
<a name="comment"></a><textarea name="tbContent" id="tbContent" class="index_main_left_6_left_ceneter_textarea" rows="5">