You need to check if the server implementation used supports firing tasks like this. If it doesn't support it or you'd like to be server independent, then implement a ServletContextListener
to hook on webapp's startup and use ScheduledExecutorService
to execute a task at the given time and intervals.
Here's a basic kickoff example:
public class Config implements ServletContextListener {
private ScheduledExecutorService scheduler;
public void contextInitialized(ServletContextEvent event) {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Task(), millisToNext1000, 1, TimeUnit.DAYS);
}
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdown();
}
}
Where Task
implements Callable
and millisToNext1000
is the amount of millis to next 10:00 AM. You can use Calendar
or JodaTime to calculate it. As a non-Java-standard alternative, you can also consider to use Quartz.