0

I'm writing a web application in GWT. Currently the only server-side logic I have are some RPC calls contained in a class that extends RemoteServiceServlet. The structure of this class looks like this:

public class ProjectActionsImpl extends RemoteServiceServlet
                                implements ProjectActions {
    public ProjectActionsImpl() {
        ... *lots* of preparations ...
    }

    public String action1(String request) {
        ...
    }

    public String action2(String request) {
        ...
    }
    ...
}

But I just realized that I have too much initialization work in the constructor, that the first call would take tens of seconds to response. I can imagine it would be annoying for the first user.

Is there a way to initialize the back-end at the moment the server starts, and before the server starts listening?

xzhu
  • 5,675
  • 4
  • 32
  • 52

1 Answers1

1

You could try with a ServletContextListener. Check this : http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/ . In short, you define a context listener that gets triggered when the webapp starts up, and there you can initialize your server-side logic. Here's an example :

public class MyServletContextListener implements ServletContextListener{

  @Override
  public void contextInitialized(ServletContextEvent ctxEvt) {
        System.out.println("this runs at web app startup"); 
  }

  @Override
  public void contextDestroyed(ServletContextEvent ctxEvt) {
    System.out.println("this runs when you stop the app");
  }

}

Then you register it in the web.xml, using the <listener> and <listener-class> tags (If you use servlet spec v.3, you can annotate the class with @WebListener instead).

francesco foresti
  • 2,004
  • 20
  • 24