天天看點

java定時器的具體使用和web使用

 公司部分項目提出将某些項目進行靜态化...是以在測試頁面命中率和資料庫通路量之後針對某些寫的頻率比較低和通路比較大頁面進行靜态化。當然也不是進行實時的靜态化.這裡需要使用定時器來進行靜态化的控制.下面了解一下定時器的應用!

1.具體方法的了解

(1)timer.schedule(timertask task,date time)//安排在制定的時間執行指定的任務。  

(2)timer.schedule(timertask task,date firsttime ,long period)//安排指定的任務在指定的時間開始進行重複的固定延遲執行.  

(3)timer.schedule(timertask task,long delay)//安排在指定延遲後執行指定的任務.  

(4)timer.schedule(timertask task,long delay,long period)//安排指定的任務從指定的延遲後開始進行重複的固定延遲執行.  

(5)timer.scheduleatfixedrate(timertask task,date firsttime,long period)//安排指定的任務在指定的時間開始進行重複的固定速率執行.  

(6)timer.scheduleatfixedrate(timertask task,long delay,long period)//安排指定的任務在指定的延遲後開始進行重複的固定速率執行.  

2.簡單的測試方法

java代碼  

import java.io.ioexception;   

import java.util.timer;   

public class timertest {   

public static void main(string[] args){   

timer timer = new timer();   

timer.schedule(new mytask(), 1000, 2000);//在1秒後執行此任務,每次間隔2秒  

   while(true){//這個是用來停止此任務的,否則就一直循環執行此任務了  

   try {   

    int ch = system.in.read();   

    if(ch-'c'==0){   

     timer.cancel();//使用這個方法退出任務   

    }   

   } catch (ioexception e) {   

    // todo auto-generated catch block

    e.printstacktrace();   

   }   

}   

static class mytask extends java.util.timertask{   

   @override  

   public void run() {   

    // todo auto-generated method stub

    system.out.println("我是來測試的。。我兩秒出來一次");   

}  

這樣,每隔兩秒鐘就會在控制台輸出語句。

3.根據實際需要進行例子測試

由于我們重新整理一些頁面或者資料的時候.都會選擇在一天的某個時候或者幾天!這裡就需要針對某個固定時間來制作這個定時器

    /**  

     * 每天固定時間更新  

     * 每天9點14分  

     */  

    @test  

    public void testregular() {   

        new date();   

        timer timer = new timer();   

        timer.schedule(new mytask(getregulardate("hh:mm", "11:21")),getregulardate("hh:mm", "11:21"));//每天固定時間更新,每天11點21分  

     *   

     * @author chenjinjie  

     *  

    static class mytask extends java.util.timertask{   

        private date time;   

        public mytask() {   

        }   

        public mytask(date time) {   

            this.time = time;   

       @override  

       public void run() {   

        if(new date().after(time))//這裡主要是防止伺服器重新開機之後發覺時間舊了會自動重新整理一次  

        system.out.println("我是來測試的。。固定時間"+time);   

       }   

     /**  

     * 根據日期字元串擷取日期對象,精确到當天淩晨零點零分,字元串格式:"yyyy-mm-dd" 

     * @param fomatstr 如(yyyy年mm月dd日 hh:mm)

     * @param datesource 源日期(string)  

     * @return  

    public static date getregulardate(string formatdata,string datestring){   

        simpledateformat   formatter   =   new   simpledateformat   (formatdata);      

        date d = null;   

        try {   

            d = formatter.parse(datestring);   

        } catch (parseexception e) {   

            e.printstacktrace();   

        calendar calendar = calendar.getinstance();   

        calendar.set(calendar.hour_of_day, d.gethours());   

        calendar.set(calendar.minute, d.getminutes());   

        calendar.set(calendar.second, d.getseconds());   

        date time = calendar.gettime();   

        return time;   

以上隻是初步簡單的使用..

4.定時器在jsp中的應用。在jsp中可以靠監聽器和定時器的結合來解決某些問題

4.1 先來一個定時器的任務:

public class task extends timertask {    

      private static final int c_schedule_hour = 0; 

      private static boolean isrunning = false;    

      private servletcontext context = null;    

      public task() {    

      }    

      public task(servletcontext context) {    

        this.context = context;    

      public void run() {    

        calendar cal = calendar.getinstance();    

        if (!isrunning) {    

            context.log("開始執行指定任務");    

            system.out.println("執行定時任務");   

            context.log("指定任務執行結束");    

        }    

        else {    

          context.log("上一次任務執行還未結束");    

      }  

4.2然後監聽器(注意實作的接口):

public class contextlistener    

extends httpservlet    

implements servletcontextlistener {    

public contextlistener() {    

}    

private java.util.timer timer = null;    

public void contextinitialized(servletcontextevent event) {    

timer = new java.util.timer(true);    

event.getservletcontext().log("定時器已啟動");    

timer.schedule(new task(event.getservletcontext()), 0, 10*60*1000); //開啟後.每10分鐘重新整理一次  

event.getservletcontext().log("已經添加任務排程表");    

public void contextdestroyed(servletcontextevent event) {    

timer.cancel();    

event.getservletcontext().log("定時器銷毀");    

}   

4.3最後配置xml檔案。

web-app 标簽内添加

<listener>

    <listener-class>cn.read.web.listener.contextlistener </listener-class>

</listener>

這樣啟動tomcat後就會在控制台輸出目前日期。。

5.項目的具體使用和感想

這隻是在項目的初步規劃.可能實行過程中還會出現很多問題.比如網絡和伺服器重新開機等...不過會在後面開發中繼續努力改進.使其功能更加完善.