Hello Everyone,

in my web app I use Java 7 directory watcher feature.

When the final war file is placed into Webapps folder and Tomcat is started,
it freezes as it waits forever for any directory changes (the app is loaded
on tomcat startup).

I use the following workaround - calling my watcher code in a new Thread (in
the context init phase).

It does the job, but I am not sure if this is the best solution. Couldn't be
the same effect achieved using specific tomcat options?

What do you think?

Thanks, Jan


package my.util.jobticketprocessor;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ContextListener implements ServletContextListener {

    private static final Logger LOGGER =
LoggerFactory.getLogger(ContextListener.class.getName());
    private Thread threadA;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        LOGGER.info("Watcher - right before the init.");

        threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                LOGGER.info("Thread - right before the init.");
                JobTicketWatcher.getInstance().initProcessor();
                LOGGER.info("Thread - right after the init.");
            }
        }, "Thread A");

        threadA.start();
        
        LOGGER.info("Watcher has been initialized successfully.");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        JobTicketWatcher.getInstance().terminateProcessor();
        try {
            threadA.join();
            LOGGER.info("Thread has been terminated.");
        } catch (InterruptedException e) {
            LOGGER.error("Thread terminating has failed.");
        }
        LOGGER.info("Watcher has been terminated.");
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to