Modifying and change Tomcat connector configuration at Shutdown from Java (for Graceful Shutdown)

5/18/2020

I have a Spring boot application, but yet our system creates a War file instead and deploy the same on the external tomcat Container which gets deployed on the kubernetes pods. So my requirement is when the pods get deleted, the application listens to the shutdown event, change the maxKeepAliveRequests = 1. So that the pod does not accept any new requests and finish executing current requests after which it becomes ready to be deleted.

Now I have add preStop lifecycle to initiate shutdown on the tomcat container when the pod gets deleted --

        lifecycle:
          preStop:
            exec:
              command: [ "/usr/local/tomcat/bin/shutdown.sh && sleep 30" ]

I am hoping with this contextDestroyed method gets triggered I have created a bean of this java class -

public class GracefulShutdown implements ServletContextListener {

    private OilSlf4jLogger logger = new OilSlf4jLogger(LoggerFactory.getILoggerFactory());

    @Override
    public void contextInitialized(ServletContextEvent sce) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.handleEvent(EventSeverity.INFO,"Initialising Graceful Shutdown");
        // .....
        // How to configure maxKeepAliveRequests so that the service does not accept any more new requests ????
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Now in this method I would like know how to configure maxKeepAliveRequests of Connector so that the service does not accept any more new requests. All the inflight requests gets completed.

If there is any simpler approach, please do let me know.

-- user93726
java
kubernetes
spring
tomcat8

0 Answers