I want to call parsePage() method every 2 hours, the method will process the page and update the information in database. I want to put this in a Tomcat server and my code looks like this right now:
TimerTask parserTimerTask = new TimerTask() {
@Override
public void run() {
MyParser.parsePage();
}
};
Timer parserTimer = new Timer();
parserTimer.scheduleAtFixedRate(parserTimerTask, 0, PERIOD);
I put this in class(that load on start up)’s init() method.
Am I doing this correctly? Or is there better way to do this task?
Thanks.
Your approach should work.
You’ll obviously also need to create a Timer and pass the TimerTask to the appropriate schedule method in order to kick off the execution of the TimerTask in a repeated manner, something like:
In more sophisticated environments, you might want to look at using something like the Quartz Scheduler. This will give you things like better logging, redundancy failover, more sophisticated schedules, transaction control etc. But all this probably isn’t necessary for a simple use case.