0

I want my web server (Tomcat 8.5) to send emails automatically once every day while it's running. So I configured a StartUp-Servlet in web.xml to run on server startup. When I test it in eclipse, I get the error Starting Tomcat v8.5 Server at Localhost has encountered a problem. Server Tomcat was unable to start within 45 seconds...

I know, this is something happening in eclipse, but it seems, that the startup doesn't finish, and my server will never be able to to other tasks as it's busy starting up... I want it to instantiate the scheduler, but then finish start up and run the scheduler 'in background'.

What is the right way to trigger the scheduler during server start?

Merha
  • 119
  • 13

1 Answers1

0

I found a way to do this. I implement a ServletContextListener instead. web.xml looks like this:

<listener>
    <listener-class>servlets.SendEmailJob</listener-class>
</listener>

and the Class:

public class SendEmailJob implements ServletContextListener {
    Scheduler scheduler = new Scheduler();

    public void contextInitialized(ServletContextEvent event) {
        scheduler.schedule("* * * * *", new Runnable() {
            public void run() {
                //sendEmail() - time pattern not specified yet
            }
        });
        // Starts the scheduler.
        scheduler.start();
    }

    public void contextDestroyed(ServletContextEvent event) {
        scheduler.stop();
        scheduler = null;
    }
}
Merha
  • 119
  • 13