views:

59

answers:

1

Hey fellows I hope you are doing well!

So the problem is like this: I have a web server and some data storage kind of servers. What I need is synchronization between both the servers. Web servers are always up, and I try to keep the data servers up as long as possible.

Both have mysql databases and I already have written scripts to synchronize them. I have made a java program which gets the new data fields from the web server and stores them in its own database every 10 seconds. An unlimited java thread does this. This would be the piece of code which does this:

Timer timer;
Date current_date = new Date() ;
    timer = new Timer();
    timer.scheduleAtFixedRate(new doCheck(), current_date, 10 * 1000);

The constructor of the class doCheck() is fetching data from the web server in XML format. No mysql direct connection b/w the servers is made.

THE REAL PROBLEM:

But when this script runs 24/7, after a certain period like in a couple of days, the script (basically a thread fetching xml from a web server) suddenly stops running and the synchronization totally disconnects.

Possibility #1: Internet disconnection: I tried unplugging the network cable, and it started giving exceptions, but as soon as I reconnected the wire, it start working perfectly fine.

Possibility #2: Any exception in the code etc.: I am maintaining logs, but most of the times it stops suddenly without and kind of exception or error.

Possibility #3: Manual cancellation of thread: No one touches the servers :P and there is just one button to cancel it, so this is not possible.

Is there any experienced programmer to tell me why does this happen?? I just want this script to run unlimited times.

+6  A: 

I can't offer a concrete reason for this would be happening, but the Timer only has one internal thread and if this Thread dies then it does not get automatically restarted. So if you have an exception thrown out of the run() method of a TimerTask just once then the Timer is dead forever.

You may be missing this exception so I would do the following

  • Implement the default thread exception handler and make sure to log the result somewhere where you can find it
  • Optionally implement a try/catch/rethrow block in your TimerTask implementation. Don't swallow the exception here, just log it and rethrow it (for now, see below).

If there is an exception being thrown out of your TimerTask you should see it twice, once in the TimerTask try/catch and once by the default exception handler when the Timer thread dies. This will confirm that the Timer thread is being killed by the exception.

Assuming that is the case then you'll have to look at the exception and decide how you want to handle it. You can catch that specific exception type and log/swallow it (modify your TimerTask implementation to suit), or maybe let the exception propogate and raise an alert in the system.

It might be worth switching to use the ScheduledExecutorService introduced in 1.5 as it is bit more useful and MAY be more robust if the a scheduled task throws an exception (I'm not 100% on this, check the relevant API docs for the implementation).

Mike Q