Reputation: 3111
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.
Upvotes: 1
Views: 1126
Reputation: 166
There is not much information here but it might make more sense in a linux environment to use a cron job to call curl or wget to a servlet?
Upvotes: 1
Reputation: 106351
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:
Timer timer=new Timer(true);
timer.schedule(myTimerTask, 0, 2*3600*1000L);
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.
Upvotes: 2