This question already has an answer here:
-
How to run a background task in a servlet based web application?
5 answers
Have to call the servlet periodically.(like service concept in andorid)
How to do this. Using timer or any other solution?
Thanks in advance.
To expand on the comments by JB Nizet…
The formerly accepted answer is kind of a hack. If the goal is to get some regular task to be performed as part of your web app, Java provides a couple of slick technologies to make this happen.
ServletContextListener
The first is a hook defined by the Servlet spec to have code invoked when a web app is deployed and when a web app is shutting down. This hook is the ServletContextListener.
ScheduledExecutorService
The second piece is the executor service added to recent versions of Java as a more sophisticated alternative to the old Timer class. In particular you need the ScheduledExecutorService.
So when your web app start up, the ServletContextListener launches a ScheduledExecutorService. When the ServletContextListener is informed of a shutdown, it tells the executor to gracefully quit.
One trick to this: Be sure to capture all exceptions in your executor. If an exception leaks, the executor silently stops executing. This is a feature not a bug. Read the doc and study up with some googling.
Search StackOverflow for examples and discussion of both of these.
You can use ,
response.addHeader("Refresh", "5");
"5" denotes 5 seconds
.The response.addHeader("Refresh", "5")
call adds a response header that is sent back to the client indicating that the browser should make another request of the servlet after 5 seconds.
Same thing can be done using html too,
<meta http-equiv="refresh" content="5" />
you can aslo pass the url here as ,
<meta http-equiv="refresh" content="5" url="example.com"/>
Also have a look at here ..
If you use Java EE 6, an EJB can be packaged in the war, and you can use a @Schedule annotated EJB.
It will be executed periodically, you don't have to do anything else (run threads, or sleep).
You can do this using Java Threads.
Schedule a servlet to load on server startup. See How can I set a servlet to load on startup of the container, rather than on the first request?
In this servlet's init() method, spawn a thread.
- In this thread's run() method, calculate the number of milliseconds to wait until the task should be performed. Then call Thread.sleep(msec). Then do the task, and loop (infinitely).
The java.util.Timer class can help a lot.
Use the schedule() method of the java.util.Timer class:
long now = System.currentTimeMillis();
Date whenToRun = new Date(now+millisecondsInFuture);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
// job code here
}
};
timer.schedule(task, whenToRun);
Or use the scheduleAtFixedRate() method of the java.util.Timer class:
int initialDelay = 30000; // start after 30 seconds
int period = 5000; // repeat every 5 seconds
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
// job code here
}
};
timer.scheduleAtFixedRate(task, initialDelay, period);