Background Thread for a Tomcat servlet app [duplicate]

If you want to start a thread when your WAR is deployed, you can define a context listener within the web.xml:


Then implement that class something like:

public class MyServletContextListener implements ServletContextListener {

    private MyThreadClass myThread = null;

    public void contextInitialized(ServletContextEvent sce) {
        if ((myThread == null) || (!myThread.isAlive())) {
            myThread = new MyThreadClass();

    public void contextDestroyed(ServletContextEvent sce){
        try {
        } catch (Exception ex) {

Leave a Comment