电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本
换一换
首页 金锄头文库 > 资源分类 > DOCX文档下载
分享到微信 分享到微博 分享到QQ空间

Java Web定时器使用(转载)

  • 资源ID:470093215       资源大小:33.63KB        全文页数:18页
  • 资源格式: DOCX        下载积分:15金贝
快捷下载 游客一键下载
账号登录下载
微信登录下载
三方登录下载: 微信开放平台登录   支付宝登录   QQ登录  
二维码
微信扫一扫登录
下载资源需要15金贝
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
支付方式: 支付宝    微信支付   
验证码:   换一换

 
账号:
密码:
验证码:   换一换
  忘记密码?
    
1、金锄头文库是“C2C”交易模式,即卖家上传的文档直接由买家下载,本站只是中间服务平台,本站所有文档下载所得的收益全部归上传人(卖家)所有,作为网络服务商,若您的权利被侵害请及时联系右侧客服;
2、如你看到网页展示的文档有jinchutou.com水印,是因预览和防盗链等技术需要对部份页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有jinchutou.com水印标识,下载后原文更清晰;
3、所有的PPT和DOC文档都被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;下载前须认真查看,确认无误后再购买;
4、文档大部份都是可以预览的,金锄头文库作为内容存储提供商,无法对各卖家所售文档的真实性、完整性、准确性以及专业性等问题提供审核和保证,请慎重购买;
5、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据;
6、如果您还有什么不清楚的或需要我们协助,可以点击右侧栏的客服。
下载须知 | 常见问题汇总

Java Web定时器使用(转载)

这个类最终功能是每天某个时间点(如每晚22点)执行某一功能.首先介绍java定时器(java.u til .Timer )有定时执行计划任务的功能,通过设定定时器的 间隔时间,会自动在此间隔时间后执行预先安排好的任务(java.u til. TimerTask)如 :每隔一个小时执行任务 timer.schedule(TimerTask, 0, 60 * 60 * 1000);schedule方法的第一个参数是需要执行的任务,此类的类型为java.u til.TimerTask, 第二个参数为执行任务前等待时间,此处0表示不等待,第三个参数为间隔时间,单位为毫秒 由于我们希望当Web工程启动时,定时器能自动开始计时,这样在整个Web工程的生命期 里,就会定时的执行任务,因此启动定时器的类不能是一般的类,此处用Servlet的监听器类来启 动定时器,通过在配置文件里配置此监听器,让其在工程启动时自动加载运行,存活期为整个 Web工程生命期.要运用Servlet侦听器需要实现javax.servlet.ServletContextListener接口,以下是类 设计:public class TimerListener implements ServletContextListener private Timer timer = null;private SampleTask sampleTask;0verridepublic void contextDestroyed(ServletContextEvent event) timer.cancel();event.getServletContext().log("定时器销毁");Overridepublic void contextInitialized(ServletContextEvent event) timer = new Timer(true);sampleTask = new SampleTask(event.getServletContext();event.getServletContext().log("定时器已启动");timer.schedule(sampleTask, 0, 60 * 60 * 1000);event.getServletContext().log ("已经添加任务调度表");public class SampleTask extends TimerTask privateServletContext context;privatestatic boolean isRunning = false;privatestatic boolean flag = true;privatestatic final int C_SCHEDULE_HOUR = 15;public SampleTask(ServletContext context) this.context = context;Overridepublic void run() Calendar cal = Calendar.getlnstance();if (!isRunning)&& flag)if ( C SCHEDULE HOUR = cal.get(Calendar. HOUR OF DAY) isRunning = true;context.log("开始执行指定任务”);/需要执行的代码isRunning = false;flag = false; context .log("指定任务执行结束");else context .log("上一次任务执行还未结束");要使用此监听器需要在web.xml中配置,如下:<listenerlistener-class包路径.TimerListener/listener-class /listener这样在web工程启动时,就会自动启动此监听器.JAVA中Timer定时器调度方法java timer中的时间调度方法主要有: schedule(TimerTask task, Date firstTime, long period)Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.但是如果此时的firstTime小于(时间落后于)当前时间,那么task会立即执行,在调试的时候不方便,因为程序一启动就开始执行了,或许 还没有到任务的触发点。schedule(TimerTask task, long delay, long period)Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.如果采用设置delay时间的方法,则可以进行处理。比如:设置执行时间为每天的13:50,如果启动的时候时间已经过了 13: 35,那 么应该在明天的13: 35进行执行。此时可以这样处理:Calendar cal = Calendar.ge tlnst ance();cal.se t(Calendar.HOUR_OF_DAY, 13);cal.se t( Calendar.MINUTE, 35); /cal.add(Calendar.DAY_OF_MONTH, 1);Date date = new Dat e();date = cal.ge tTime();Date now = new Dat e();long int erval = dat e.ge tTime() - now.ge tTime(); /should exec in next dayif (interval < 0) cal.add(Calendar.DAY_OF_MONTH, 1); date = cal.ge tTime();int erval = dat e.ge tTime() - now.ge tTime();System.out .println("the interval time is: " + interval); /the exec time interval is 2 secs.ti mer.schedule(echoTask, int erval, 2 * 1000);如果delay的时间为负数,会报异常,因此,Calendar添加一天。JAVA WEB程序中添加定时器/这是我的定时器类,用来定时执行某段任务;package com.my .ti me;import java .text .ParseException;import java .text .SimpleDateFormat;import java. util.Date;import java. util.Timer;public class BugXmlTimer public Timer timer;public void ti merS tart()ti mer = new Timer();Date dateti me=new Dat e();Date midnigh tDat e=new Dat e();SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try midnigh tDate = sdf2.parse(sdf1.forma t(dateti me)+" 23:00:00"); catch (ParseException e) / TODO Auto-generated catch blocke.pri ntSt ackTrace();long in二midnigh tDat e.ge tTime()-da teti me.ge tTime();Sys tem.o ut .pri ntln ("before t ask");/立刻执行,然后每隔30s执行一次ti mer.schedule(new BugXmlTimerTask(), 0,30000);public void ti merS to p()if(ti mer!二null)ti mer.cancel();public static void main(S tring args)BugXmlTimer myTimer二new BugXmlTimer();/ TODO Auto-generated method stubmyTimer .ti merS tart();/这是执行任务的类,即每隔一段时间要做的事情在这里 package com.my .ti me;import java. util.TimerTask;public class BugXmlTimerTask extends TimerTask Overridepublic void run() Sys tem.o ut .pri nt( "run t ask");/以下是出发定时操作的类,该类实现了 ServletContextListenerpublic class MyTimerListener implements ServletContextListener private BugXmlTimer mytimer = new BugXmlTimer ();public void contextlnitialized(ServletContextEvent event) myti mer .ti merS tart();public void contextDestroyed(ServletContextEvent event) myti mer .ti merS to p();然后在web.xml里部署一下,即可在程序启动后运行定时器了!<listener><listener-class>com.my .time.MyTimerListener </listener-class></listener>Java定时器退出解决方案项目中用到了 Timer每隔一段时间进行一些操作,现在发现有时候莫名其妙地 挂在这个计时器上的操作都不做了,用“JConsole”查看其中的线程以后,发现 这个定时器线程也不在了(定时器创建的时候带了名字Timer timer = new Timer("MyTimer"),所以直接能看到),由于日志太多,之前的日志已经找不到 了,所以没有办法看是否是有异常发生以及发生在哪里。初步估计,是不是由于 TimerTask中有异常抛出,如果定时器中没有处理的话,可能就会出问题。所以 看了一下 java. util.Timer 的代码:1. /在TimerThread中执行任务2. Timer.java:101:TimerThread3. / TimerThread

注意事项

本文(Java Web定时器使用(转载))为本站会员(cn****1)主动上传,金锄头文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即阅读金锄头文库的“版权提示”【网址:https://www.jinchutou.com/h-59.html】,按提示上传提交保证函及证明材料,经审查核实后我们立即给予删除!

温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.