I want to schedule a method call in Spring MVC to run after every two hours. This I can easily do via Spring 3.0 Time Scheduler. However, I want to kick the execution off only at a specific time of the day. That is, the method should only be invoked every 2 hours starting at a particular time.
For example – I want the method to run every 2 hours starting 6 AM.
The TimeScheduler interface has a scheduleAtFixedRate method which is overloaded to use startTime Date argument. I am not really sure how to use this.
Any idea how this can be achieved ?
You could take a look at the
TaskSchedulerinterface. It provides a methodscheduleAtFixedRate(Runnable task, Date startTime, long period)which returns aScheduledFuture. You can use this with some simple Spring configuration:This will create an instance of
ThreadPoolTaskSchedulerwhich implementsTaskScheduler. Wire this bad boy into the class to call your specific method: