KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tasks > shutdown > ShutdownTimerTask


1 package com.sslexplorer.tasks.shutdown;
2
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.TimerTask JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import com.sslexplorer.boot.ContextHolder;
11
12 public class ShutdownTimerTask extends TimerTask JavaDoc {
13
14     final static Log log = LogFactory.getLog(ShutdownTimerTask.class);
15
16     public static String JavaDoc NAME = "shutdown.task";
17     
18     boolean restart = false;
19     long defaultDelay = 10000;
20     long delay = 0;
21     
22     public ShutdownTimerTask(boolean restart, int delayInMins) {
23         super();
24         this.restart = restart;
25         this.delay = (delayInMins*60*1000) ;
26     }
27
28     public void run() {
29         if (log.isInfoEnabled())
30             log.info("About to perform shutdown task.");
31         ContextHolder.getContext().shutdown(restart);
32     }
33
34     public long getDelay() {
35         // this is so the minimum shutdown time is 10 seconds.
36
if (delay < defaultDelay)
37             return defaultDelay;
38         else
39             return delay;
40     }
41
42     public void setDelay(long shutdownTime) {
43         this.delay = shutdownTime;
44     }
45
46     public Date JavaDoc getShutDownTime(){
47         return new Date JavaDoc(System.currentTimeMillis() +this.getDelay());
48     }
49     
50     public String JavaDoc getShutDownTimeString(){
51         return getShutDownTimeString("HH:mm");
52     }
53
54     public String JavaDoc getShutDownTimeString(String JavaDoc pattern){
55         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(pattern);
56         return sdf.format(getShutDownTime());
57     }
58     
59 }
60
Popular Tags