国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

一個簡單的Timer Service

2019-11-18 15:59:58
字體:
供稿:網(wǎng)友

  Web-TimeService用于定時調(diào)用(觸發(fā))應(yīng)用,EJB2.1也提供了TimerService,但現(xiàn)在有的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,并每到一定時候取出來看看是否該調(diào)用
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
* 該例子功能是在天天的凌晨一點調(diào)用
*/
public class TimerJobExample implements TimerJob
{
 Calendar nextDate = null;
 public TimerJobExample()
 {
  nextDate = Calendar.getInstance();
  nextDate.add(Calendar.DAY_OF_MONTH,1);
  //將設(shè)置調(diào)用時間是(第二天的)天天凌晨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?

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪雅县| 延川县| 郑州市| 米泉市| 沧州市| 阿瓦提县| 常山县| 雅安市| 武邑县| 遂宁市| 绥德县| 延长县| 济源市| 宜兴市| 涟源市| 罗江县| 多伦县| 贵南县| 株洲县| 西丰县| 正宁县| 饶平县| 工布江达县| 西乌珠穆沁旗| 岳阳市| 荃湾区| 成安县| 左权县| 吴江市| 阜阳市| 石嘴山市| 青州市| 云霄县| 大洼县| 石门县| 雅江县| 昌黎县| 忻城县| 洛扎县| 青河县| 新绛县|