java timer监听器_Java中用Servlet Listener实现定时监听 (Timer、TimerTask)

转载自: http://www.zjsyc.com/blog/article/J2EE/318.html

分两步走:

(1)实现 javax.servlet.ServletContextListener 接口的两个方法:contextInitialized()和contextDestroyed()

contextInitialized():当Servlet容器启动时会执行contextDestroyed():当Servlet容器停止时会执行

(2)在contextInitialized()中加入需要监听的程序,并由 java.util.Timer 的 schedule() 方法来控制监听程序执行的频率

DEMO(这是我的一个短信回复监听的程序原型,精简了一下)

----------------------------------------------------------------

ReplyListener.java

----------------------------------------------------------------

830fb7c6db0c54ee3f34db9987903ca2.gifpackage com.hanweb.jcms;

830fb7c6db0c54ee3f34db9987903ca2.gif

830fb7c6db0c54ee3f34db9987903ca2.gifimport javax.servlet.*;

830fb7c6db0c54ee3f34db9987903ca2.gif

b72172b39b9813342304bc9563cc8442.gif

fbb06bf7567652e09ffb41f84bb73096.gifpublic class ReplyListener implements ServletContextListener 

918e8df969f9f8c8d002f25cda86cade.png{

383309e21eea0a01753a5af2c98860ff.gifprivate ReplyTimer rt = null;

7b10586d04e03985df3a99ae560d9545.gif

c36f50728e36843ebb91ab7d5c68b56f.gifpublic void contextInitialized(ServletContextEvent event) 

918e8df969f9f8c8d002f25cda86cade.png{

383309e21eea0a01753a5af2c98860ff.gifString status = "[SYS] SMS reply listener start .";

383309e21eea0a01753a5af2c98860ff.gifevent.getServletContext().log(status);

383309e21eea0a01753a5af2c98860ff.gifSystem.out.println(status);

383309e21eea0a01753a5af2c98860ff.gif

383309e21eea0a01753a5af2c98860ff.gifrt = new ReplyTimer(1);

383309e21eea0a01753a5af2c98860ff.gifrt.start();

a7ee29d38b23955c3a02da2f5455980c.gif}

383309e21eea0a01753a5af2c98860ff.gif

7b10586d04e03985df3a99ae560d9545.gif

c36f50728e36843ebb91ab7d5c68b56f.gifpublic void contextDestroyed(ServletContextEvent event) 

918e8df969f9f8c8d002f25cda86cade.png{

383309e21eea0a01753a5af2c98860ff.gifString status = "[SYS] SMS reply listener stop .";

383309e21eea0a01753a5af2c98860ff.gifevent.getServletContext().log(status);

383309e21eea0a01753a5af2c98860ff.gifSystem.out.println(status);

383309e21eea0a01753a5af2c98860ff.gif

7b10586d04e03985df3a99ae560d9545.gif

c36f50728e36843ebb91ab7d5c68b56f.gifif (rt != null) 

918e8df969f9f8c8d002f25cda86cade.png{

383309e21eea0a01753a5af2c98860ff.gifrt.stop();

a7ee29d38b23955c3a02da2f5455980c.gif}

a7ee29d38b23955c3a02da2f5455980c.gif}

7679b6b6bf59c2e9cd740975e6021a7e.gif}

----------------------------------------------------------------

ReplyTimer.java

----------------------------------------------------------------

cbef093dcc044b2793832001e2365e43.pngpackage com.hanweb.jcms;

cbef093dcc044b2793832001e2365e43.png

cbef093dcc044b2793832001e2365e43.pngimport java.util.*;

cbef093dcc044b2793832001e2365e43.png

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngpublic class ReplyTimer

918e8df969f9f8c8d002f25cda86cade.png{

df37983f39daa189b8c814e01a6a9011.pngprivate final Timer timer = new Timer();

df37983f39daa189b8c814e01a6a9011.pngprivate final int min;

df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngpublic ReplyTimer(int minutes)

918e8df969f9f8c8d002f25cda86cade.png{

df37983f39daa189b8c814e01a6a9011.pngmin = minutes;

4a5daaec04350a363f186a4d2c5ed6ce.png}

df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngpublic void start()

918e8df969f9f8c8d002f25cda86cade.png{

df37983f39daa189b8c814e01a6a9011.pngDate date = new Date();

df37983f39daa189b8c814e01a6a9011.pngtimer.schedule(new ReplyTask(), date, min * 60 * 1000);

4a5daaec04350a363f186a4d2c5ed6ce.png}

df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngpublic void stop()

918e8df969f9f8c8d002f25cda86cade.png{

df37983f39daa189b8c814e01a6a9011.pngtimer.cancel();

4a5daaec04350a363f186a4d2c5ed6ce.png}

0ac3a2d53663ec01c7f7225264eeefae.png}

----------------------------------------------------------------

ReplyTask.java

----------------------------------------------------------------

cbef093dcc044b2793832001e2365e43.pngpackage com.hanweb.jcms;

cbef093dcc044b2793832001e2365e43.png

cbef093dcc044b2793832001e2365e43.pngimport java.util.*;

cbef093dcc044b2793832001e2365e43.png

2f88ce130b654eb5dc6788e02dbcfc90.png

dbf989d57862681739b642d8621fe1f0.pngpublic class ReplyTask extends TimerTask

918e8df969f9f8c8d002f25cda86cade.png{

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngpublic void doSomething()

918e8df969f9f8c8d002f25cda86cade.png{

df37983f39daa189b8c814e01a6a9011.pngSystem.out.println("[SYS] SMS reply listener running ");

4a5daaec04350a363f186a4d2c5ed6ce.png}

df37983f39daa189b8c814e01a6a9011.png

f70a0fde2b51b7dd92a70e712e540cf6.png

edb48e6f68462ea23d9a824f01de40c5.pngpublic void run()

918e8df969f9f8c8d002f25cda86cade.png{

df37983f39daa189b8c814e01a6a9011.pngdoSomething();

4a5daaec04350a363f186a4d2c5ed6ce.png}

0ac3a2d53663ec01c7f7225264eeefae.png}

将编译好的class文件放入WEB-INF/classes中,最后别忘记了在Servlet容器中当前WEB应用的web.xml中加入监听语句:

cbef093dcc044b2793832001e2365e43.png

cbef093dcc044b2793832001e2365e43.pngcom.hanweb.jcms.ReplyListener

cbef093dcc044b2793832001e2365e43.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值