• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java util Timer terminates itself

 
sram bahl
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic