站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个简单的Timer Service

一个简单的Timer Service

摘要: Web-TimeService用于定时调用(触发)应用,EJB2.1也提供了TimerService
<table width="570" border="0" cellspacing="0" cellpadding="5" bgcolor="FBE392"> <tr> <td> <table width="570" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="E1B004" width="200" align="center">天极IT资讯短信服务 电脑小技巧
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="fffcc0"> function check4() { if (dn.mobile.value.length!=11) { alert("手机号码不正确!"); dn.mobile.focus(); return false; } return true; } <form name=dn action="http://www.my5757.com/tj/join.jsp" target=_blank onSubmit="return check4()"> <tr valign=middle> <td>资费:包月5元
手机: <input type=text name=mobile size=11 style="border:1px solid #000000;height=16px"> <input type=image src="http://www.my5757.com/yesky/images/d34.gif" border="0" width="45" height="18" align="middle" name="image2" > </td> </tr> <input type=hidden name=stype value="yjq"> </form> </table> </td> <td width="370" bgcolor="FBC403">介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。 </td> </tr> </table> </td> </tr> </table>

  Web-TimeService用于定时调用(触发)应用,EJB2.1也提供了TimerService,但现在有的application server不支持,有的就根本没有用到ejb,所以我写了一个简单的TimerSerivce<iframe width="360" height="300" align="center"scrolling="No" frameborder="0" marginheight="0" marginwidth="0" SRC="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html"></iframe>

<table width="100%" bgColor=#ffffff> <tr> <td>Public class TimerService
{
 public static final long p = 1000*60*60;
  Timer timer = new Timer(false);
  TimerSchedule schedule = null;
 public TimerService()
 {
 }

 public void start() throws Exception
 {
  schedule = new TimerSchedule();
  schedule.addTimerJob(new SomeTimerJob());
  //add other job here
  timer.schedule(schedule,0,p);
 }

 public void stop() throws Exception
 {
  timer.cancel();
 }
}

//包含了多个TimerJob,并每到一定时候取出来看看是否该调用
public class TimerSchedule extends TimerTask
{
 private List list = new ArrayList();
 public TimerSchedule()
 {}
 public void addTimerJob(TimerJob job)
 {
  list.add(job);
 }

 public void run()
 {
  Date now = Calendar.getInstance().getTime();
  Date next = null;
  for(int i=0;i<list.size();i++)
  {
   TimerJob job = (TimerJob)list.get(i);
   next = job.getNextExeDate();
   if(isEquals(now,next))
   {
    job.execute();
   }
  }
 }

/**
* 比较俩个时间相差是否小于TimerService.p(一个周期)
* @param now
* @param next
* @return
*/
private boolean isEquals(Date now,Date next)
{
 long time = next.getTime()-now.getTime();
 if (time <= TimerService.p && time >= 0)
 {
  return true;
 }
 else
 {
  return false;
 }
}

public boolean cancel()
{
 return true;
}
}


//该接口描述了如何完成TimerTask,请参考TimerJobExample
interface TimerJob
{
 public void execute();
 public Date getNextExeDate();
}

/**
* 该例子用于演示如何完成tiemrjob
* 该例子功能是在每天的凌晨一点调用
*/
public class TimerJobExample implements TimerJob
{
 Calendar nextDate = null;
 public TimerJobExample()
 {
  nextDate = Calendar.getInstance();
  nextDate.add(Calendar.DAY_OF_MONTH,1);
  //将设置调用时间是(第二天的)每天凌晨 1点
  nextDate.set(Calendar.HOUR_OF_DAY,1);
 }
 public void execute()
 {
  nextDate.add(Calendar.DAY_OF_MONTH,1);
  nextDate.set(Calendar.HOUR_OF_DAY,1);
  callFunction();
 }

 public Date getNextExeDate()
 { 
  return nextDate.getTime();
 }

 private void callFunction()
 {
  System.out.println("TimerJobExample call ejb funcation:"+new Date());
 }
}
</td></tr></table>
  启动Web_TimerService

  启动Web-TimerService可以有多种方法,下面列出一个简单的方法,通过jsp来启动,停止TimerService

<table width="100%" bgColor=#ffffff> <tr> <td><%@ page contentType="text/html; charset=GBK" %>
<%@ page import="com.ted.cfioms.common.alert.*"%>
<%
 TimerService service = (TimerService)application.getAttribute("timerService");
 boolean isStart = true;
 if(service == null)
 {
  service = new TimerService();
  application.setAttribute("timerService",service);
  service.start();
 }
 else
 {
  service.stop();
  isStart = false;
  service = null;
 }
%>
<html>
<head>
 <title>
  timerService
 </title>
</head>
<body bgcolor="#ffffff">
<h1>
 <%=(isStart?"start ok":"stop ok")%>
</h1>
</body>
</html></td></tr></table>
  简单吧,呵呵,我在网上没找到合适的TimerService,所以自己写的,如果大家有类似的代码,可以提出来参考参考,谢谢



↑返回目录
前一篇: Java程序设计基础入门之慨述
后一篇: X3D实战基础讲座之一