3

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
       ...
    }
...
PAX
  • 1,056
  • 15
  • 33

2 Answers2

1

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).

Jdaydai
  • 101
  • 1
  • 8
  • I precise that this code will allow you to initialize and shutdown gracefully without dealing it "manually" – Jdaydai Dec 27 '15 at 22:44
  • I think if there's no API method to trigger a shutdown of the web container, programmatically, then I need to release my resources and to finish specific processes within this shutdown hook. – PAX Dec 28 '15 at 16:57
1

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.

Dawid Pytel
  • 2,750
  • 1
  • 23
  • 30
  • To be honest, I don't care about broken HTTP request when the servlet is currently shutting down. But I'm interested in sending the web container the shutdown signal. Isn't there any convenient API? Under [Tomcat shuts down on closing jar](http://stackoverflow.com/a/25992568/2138953) I read that a call to ``System.exit(0)`` wouldn't be a propper approach. – PAX Dec 28 '15 at 16:45
  • @PAX you need to be more specific. What do you want to shut down? Single servlet (which I assumed) or whole server/jvm? What server do you use? Tomcat? – Dawid Pytel Dec 28 '15 at 17:22
  • I'm using Tomcat as web container. My goal is to gracefully shutting down the entire CXF servlet. Of course, before shutting down all resources must be released and already running thread pool tasks must be finished, first. The shutdown must effort two things: 1. Stop serving further requests, 2. Allow replacement of the underlying servlet implementation which serves the requests. Finally, the servlet could be restarted, again. – PAX Dec 28 '15 at 17:36
  • Thanks @PAX for answer. But still there is something I don't quite get. What servlet implementation do you want to replace? You are using standard CXF servlet so I don't see why should you replace it. The only idea is to be able to upgrade CXF to newer version. Or maybe you want to update services implementation? Anyway it sounds like you would like to use OSGi which is not available by default on Tomcat. See for instance https://manning.com/books/enterprise-osgi-in-action or http://stackoverflow.com/questions/16313650/how-to-use-servlet-with-osgi – Dawid Pytel Dec 28 '15 at 18:36
  • You're right, @DawidPytel, I want to update my service implementations. But I also want to be able to temporarily shutdown the service in general. You cannot assume that the server never will be stopped again. You must imagine that my service executes different operations that must be atomic. If they get aborted then you possibly leave an inconsistent state. – PAX Dec 28 '15 at 19:12
  • I updated my answer based on your comments. Please check if it fits your needs – Dawid Pytel Dec 28 '15 at 19:49