Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shutting down Apache CXFServlet

Tags:

java

shutdown

cxf

I'm executing a CXF Servlet which provides several service methods.

web.xml:

...
<servlet>
    <description>Apache CXF Endpoint</description>
    <display-name>cxf</display-name>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
...

How can I programmatically shuting down such a running server instance, properly?

public class ServiceImpl {
...
    @GET
    @Path("/shutdown/")
    void shutdown() {
       // ...releasing any resources...
       // ...terminating any threads...
       // TODO terminating running server
       ...
    }
...
like image 838
PAX Avatar asked Nov 26 '25 13:11

PAX


2 Answers

As I understand you want to make CXF servlet stop handling web service requests once you "shut it down". Unfortunately all servlets are managed by web container (e.g. Tomcat) so you cannot do it manually.

You need custom solution. I see two options:

  • extend CXFServlet and override its service method
  • write (servlet) filter

Both could check some global variable (in simplest case) and immediately return HTTP error if services were shut down. The variable should be set by ServiceImpl#shutdown.


EDIT:

On Tomcat you can also use Manager App to stop existing application. You can invoke the services from your service or directly.

This obviously stops whole application and all its servlets. I have not tested that but I am pretty sure that ongoing requests are not terminated but finish gracefully before application is stopped (at least this is how it works on some application servers like WebSphere Application Server).

If you need more fine-grained approach to stopping and updating parts of your application you might consider using OSGi. But this is topic for another question.

like image 148
Dawid Pytel Avatar answered Nov 29 '25 01:11

Dawid Pytel


Depending on your servlet spec. you've got the choice of class declaration ...

  1. Create your own class implementing the http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContextListener.html interface and declare it with @WebListener -> Then, implement the 2 methods : contextDestroyed() and contextInitialized()...

  2. Do the same without the @WebListener annotation declaration and, instead, declare the previously created class to your web.xml file (with the fully qualified class name).

like image 24
Jdaydai Avatar answered Nov 29 '25 02:11

Jdaydai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!