Java定時器Web)

來源:互聯網
上載者:User

通過定時器進行任務的執行是比較常見的情況,下面的是個簡單的樣本: 

主要藉助於監聽器和TimerTask和Timer類進行實現,另外spring提供了定時器的進階實現好像是quarts。 

package com.rx.timer; 

import java.util.Date; 
import java.util.TimerTask; 

public class MyTask extends TimerTask { 
    @Override 
    public void run() { 
        System.out.println("call at " + (new Date()));   
    } 
}
 

package com.rx.timer; 

import java.util.Timer; 
import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 

public class MyListen  implements ServletContextListener { 

    private Timer timer = null; 
     
    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 
        timer.cancel(); 
    } 

    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 
        timer = new Timer(true); 
        //設定任務計劃,啟動和間隔時間 
        timer.schedule(new MyTask(), 0, 1000 * 10); 
    } 

 

 

 

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name> 
    WebTimer</display-name> 
     
    <listener> 
            <listener-class>com.rx.timer.MyListen</listener-class> 
    </listener> 

    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

 

 

嗯...再轉個複雜點的!和上面基本相同,但是在任務執行控制上更細緻了

 

public class WorkServiceImpl implements WorkService , ServletContextListener{ 

    public void contextDestroyed(ServletContextEvent arg0){ 
       timer.cancel(); 
       System.out.println("定時器已銷毀"); 
    } 

    public void contextInitialized(ServletContextEvent event){ 
       timer = new java.util.Timer(true); 
       sampleTask =  new SampleTask(event.getServletContext()); 
       System.out.println("定時器已啟動"); 
       timer.schedule(sampleTask, 0, 60 * 60 * 1000); 
       System.out.println("已經新增工作調度表"); 
    } 

class SampleTask extends TimerTask{ 

    private ServletContext context; 
    private static boolean isRunning = false; //是否在運行
    private static boolean flag = true;       //跑過一次就不跑了
    private static final int C_SCHEDULE_HOUR = 23;  //已耗用時間
    private WorkServiceImpl workService; 

    public SampleTask(ServletContext context){ 
        this.context = context; 
    } 

    public void run() { 
    workService = new WorkServiceImpl(); 
        Calendar cal = Calendar.getInstance(); 
        if (!isRunning){ 
            if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY) && flag){ 
                isRunning = true; 
                workService.autoWorkOff(); 
                isRunning = false; 
                flag = false; 
                context.log("指定任務執行結束"); 
            } 
        } else{ 
            context.log("上一次任務執行還未結束"); 
        } 
        if(C_SCHEDULE_HOUR != cal.get(Calendar.HOUR_OF_DAY)){ 
            flag = true; 
        } 
      } 

 

在web.xml中配置,如下:
<listener> 

<listener-class>com.css.wam.service.impl.WorkServiceImpl</listener-class>

</listener>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.