You need to check if the server implementation supports such tasks. If it does not support it or you want to be server independent, then run the ServletContextListener to connect to the webapp launch and use the ScheduledExecutorService to complete the task at the specified time and intervals.
Here is an example of the main launch:
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 - the number of milliseconds in the next 10:00. You can use Calendar or JodaTime to calculate this. As an alternative to non-Java, you can also use Quartz .
source share