动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   程序开发 → 阅读文章

 一个简单的Timer Service

作者来源: 
阅读 数 305 人次 , 2006-4-28 9:27:00 


  Web-TimeService用于定时调用(触发)应用,EJB2.1也提供了TimerService,但现在有的application server不支持,有的就根本没有用到ejb,所以我写了一个简单的TimerSerivce

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());
 }
}

  启动Web_TimerService

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

<%@ 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>

  简单吧,呵呵,我在网上没找到合适的TimerService,所以自己写的,如果大家有类似的代码,可以提出来参考参考,谢谢
  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用RMI开发基于Java的企业分布式应用
· 下一篇:JFC/Swing活学活用之JLabel文字魔法
· 用Java实现FTP服务器
· Java内存泄露问题分析
· 初识 Smart Device Extensions
· Powerbuilder应用开发系列讲座(31)
· Java Applet 入门第四天


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077