Torben
Torben

Reputation: 102

Shutdownhook of a Webservice in Tomcat

I'm publishing a Webservice with Jax-ws in Tomcat

@WebService()
public class ChatService
...followed by a constructor and several public methods

The problem is how do I define a method that is called as soon as the webservice gets shutdown. I need it to stop some threads and prevent memory leaks.

And this is a part of the web.xml

<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
    <description>JAX-WS endpoint</description>
    <display-name>WSServlet</display-name>
    <servlet-name>WSServlet</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>WSServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Upvotes: 1

Views: 729

Answers (1)

Khary Mendez
Khary Mendez

Reputation: 1858

You need to define your own ContextListener and add it in a listener tag of your web.xml. Implement javax.servlet.ServletContextListener and override the contextDestroyed method. See this tutorial for details: http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/

public class CleanupContextListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO: Your cleanup code here
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // Intentionally Empty
    }
}

Upvotes: 2

Related Questions