tags:

views:

796

answers:

2

I have a standard GWT application, and it of course uses a Java servlet on the backend. This servlet is deployed on Tomcat and Windows Server.

I know it's against the rules / suggestions, but I have one thread in this servlet that get's started when the servlet initializes (the "init" method of the servlet). The thread is a scheduler of sorts, its purpose is to perform different database tasks at certain times, totally independent of the GWT application / interface itself.

What I need is for the servlet's "init" method to be called as soon as the war is deployed. Right now what I've been doing is, everytime there is an upgrade to the application, I drop the war into the right directory, then I have to "login" to the application GWT application so that its "init" method is called. I would like for the servlet's init method to be called as soon as the war is updated so that I don't have to login to the GWT application to do this.

Any ideas?

+2  A: 

you could use the servlet context listener. More specifically you could start your thread in the contextInitialized method:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
         // start the thread
    }

    public void contextDestroyed(ServletContextEvent sce) {
         // stop the thread
    }
}

then add:

<listener>
    <description>ServletContextListener</description>
    <listener-class>MyListener</listener-class>
</listener>

in you web.xml

dfa
Thank you, that's what I was looking for.
work like a charm. Thanks
Antoine Claval
+1  A: 

Another alternative would be to use the Quartz Scheduler.

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

It's very easy to use, and it's entire reason for existing is to schedule jobs. Which sounds like what you wanting to do.

Steve K
This is probably an effective solution, but right now I just need something as simple as possible, so that the least amount of changes to my code is necessary. But thanks anyways, I'll have to look into this in more detail in the future...