I have a web application deployed in Tomcat. I have a set of code in it, that checks database for certain data and then sends a mail to users depending on that data. Can somebody suggest how to schedule this in Tomcat.
Asked
Active
Viewed 3.6k times
3 Answers
28
Actually, the best way to schedule task in Tomcat is to use ScheduledExecutorService. TimeTask should not be used in J2E applications, this is not a good practice.
Example with the right way :
create a package different that you controller one (servlet package), and create a new java class on this new package as example :
// your package
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class BackgroundJobManager implements ServletContextListener {
private ScheduledExecutorService scheduler;
@Override
public void contextInitialized(ServletContextEvent event) {
scheduler = Executors.newSingleThreadScheduledExecutor();
// scheduler.scheduleAtFixedRate(new DailyJob(), 0, 1, TimeUnit.DAYS);
scheduler.scheduleAtFixedRate(new HourlyJob(), 0, 1, TimeUnit.HOURS);
//scheduler.scheduleAtFixedRate(new MinJob(), 0, 1, TimeUnit.MINUTES);
// scheduler.scheduleAtFixedRate(new SecJob(), 0, 15, TimeUnit.SECONDS);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdownNow();
}
}
After that you can create other java class (one per schedule) as follow :
public class HourlyJob implements Runnable {
@Override
public void run() {
// Do your hourly job here.
System.out.println("Job trigged by scheduler");
}
}
Enjoy :)

Benjamin Leconte
- 306
- 3
- 4
-
5A claim like "...should not be used in J2E applications, this is not a good practice." without any further backing up is - you guessed it - not a good practice as well. – zb226 Apr 04 '17 at 12:16
-
1`ScheduledExecutorService` and `cron4j` are relatively light and work well if you have only one Tomcat instance. However, this approach does not work well when you deploy and run your application in a clustered or load balanced environment, since you risk each instance running the same task. Solutions such as Quartz have mechanisms in place so that only one of the running schedulers will execute the task. – Pixelstix Dec 13 '17 at 18:04
-
@Pixelstix's comment should really be answer. You saved me headaches before implementing something in my clustered environment! – Shawn Jun 01 '18 at 14:43
8
It depends on libraries you use. Several libraries can do that:
- Quartz / Example for Tomcat.
- Spring.
- A class from Java SE.
- If you run on GAE take a look at this.
3
You can use a listener and cron4j:
@WebListener
public class StartListener implements ServletContextListener {
@Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
Scheduler scheduler = new Scheduler();
scheduler.schedule("0 * * * *", new Task());
scheduler.start();
servletContextEvent.getServletContext().setAttribute("SCHEDULER", scheduler);
}

zb226
- 9,586
- 6
- 49
- 79

Pablo Gallego Falcón
- 938
- 7
- 12