This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java util Timer terminates itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java util Timer terminates itself" Watch "Java util Timer terminates itself" New topic
Author

Java util Timer terminates itself

sram bahl
Greenhorn

Joined: Nov 26, 2002
Posts: 13
I'm using java.util.Timer class to monitor some of my application process and report to a controller. This monitor runs 24 X 7 at a fixed polling interval. What i notice is after 5-6 days the monitor terminates itself. My actual code has two monitors (SelfMonitor and AppMonitor), both running at different intervals. What could be the problem. If i add a shutdownhook is it possible to ascertain what caused the termination ? I do not call cancel anywhere on my code.

public class TimerProblem {

class SelfMonitor extends TimerTask {
/*
* Send SaJvmMonitor heartbeat to the controller every 10 mins.
*/
public void run() {
log.info("SaJvmMonitor is Alive...");
try {
conn = (HttpURLConnection) controlUrl.openConnection();
conn.setDoOutput(true);
PrintWriter out = new PrintWriter(conn.getOutputStream());
log.debug("connected to controller " + conn);
out.println(
"SaJvm Status = "
+ URLEncoder.encode("SaJvmMonitor is Alive..."));
out.flush();
out.close();
int code = conn.getResponseCode();
log.debug(String.valueOf(code));
} catch (Exception ex) {
log.warn(
"Unable to establish connection with the Controller "
+ controller);

log.info(ex.fillInStackTrace());
} finally {
out = null; //release PrintWriter for GC

}
}
}

public static void main(String[] ar) {
timer = new Timer();
timer.schedule(new AppMonitor(), 0, pulseRate * 60 * 1000);
timer.schedule(new SelfMonitor(), 0, pulseRate * 3 * 60 * 1000);
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java util Timer terminates itself
 
Similar Threads
threads for GUI and command-line
Schedular class.. Which is a better option?
Timer
Timer IllegalStateException
wait runtime object finish execution