Reputation: 1156
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
...
}
...
Upvotes: 3
Views: 564
Reputation: 2810
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:
CXFServlet
and override its service
methodBoth 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.
Upvotes: 1
Reputation: 101
Depending on your servlet spec. you've got the choice of class declaration ...
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()...
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).
Upvotes: 1