0
votes

I'm trying to add a SIGTERM support to my spring boot application. In order to test it I added a controller mapping which supposed to emulate long request:

@RequestMapping(value = "/sleep/{time}", method = RequestMethod.POST)
public void sleep(@PathVariable("time") int time) throws InterruptedException {
    int sleepLimit = 30000;
    if (time >= sleepLimit)
        throw new IllegalArgumentException("Sleep time cannot be more than " + sleepLimit + " ms.");
    Thread.sleep(time);
}

I using embedded tomcat. The problem is when sending a kill SIGTERM to the process while request is active (using CTRL+C, /shutdown endpoint or trap in a shell script inside docker), the application "closes" the test request and does not wait to the call to finish. Here is the log when calling SIGTERM:

2015-12-26 20:22:43.812  INFO 11608 --- [       Thread-8] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@548753b8: startup date [Sat Dec 26 20:22:13
 IST 2015]; root of context hierarchy
2015-12-26 20:22:43.815  INFO 11608 --- [       Thread-8] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2015-12-26 20:22:43.854  INFO 11608 --- [       Thread-8] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2015-12-26 20:22:43.856  INFO 11608 --- [       Thread-8] o.s.j.d.e.EmbeddedDatabaseFactory        : Shutting down embedded database: url='jdbc:hsqldb:mem:testdb'
2015-12-26 20:22:43.880  INFO 11608 --- [       Thread-8] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:2, serverValue:2}] to 127.0.0.1:26140 because the pool has been closed.
2015-12-26 20:22:45.093  WARN 11608 --- [       Thread-3] d.f.e.p.store.CachingArtifactStore       : Already removed de.flapdoodle.embed.process.extract.ImmutableExtractedFileSet@45524cfd for Version{3.0.2}:Windows:B64, emergency shutdown?

How can I make my application to wait for this request to end? Thanks!

1
SIGTERM uses interrupts to handle this signal, once you can write you own handler you can hook the process. - Roman C
I don't know this for a fact. I am curious about what would happen however, if you write a destroy method and annotate as predestroy on your service. Does spring wait for that to finish? because there could be a way to query for any currently in progress requests and potentially wait for those to terminate or just kill them after a certain timeout. Do tell us how it goes if you try it. - MahdeTo

1 Answers

-1
votes

Here is my solution for that:

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.buffer.BufferMetricReader;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import javax.servlet.*;
import java.io.IOException;

@Service
@Scope("singleton")
public class ActiveSessionsService {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ActiveSessionsService.class);
    @Autowired
    CounterService counterService;
    @Autowired
    BufferMetricReader metrics;

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        Filter filter = new Filter() {
            @Override
            public void init(FilterConfig filterConfig) throws ServletException {
            }

            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                try {
                    counterService.increment("active-http-sessions");
                    filterChain.doFilter(servletRequest, servletResponse);
                } finally {
                    counterService.decrement("active-http-sessions");
                }
            }

            @Override
            public void destroy() {
            }
        };
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns("/base/*");
        return registrationBean;
    }
    @PreDestroy
    public void cleanUp() throws Exception {
        int waitTime=0;
        while (waitTime < 10) {
            if(!isThereActiveRequests())
                break;
            Thread.sleep(1000);
        }
    }

    private boolean isThereActiveRequests() {
        int activeCalls =metrics.findOne("counter.active-http-sessions").getValue().intValue();
        logger.info(activeCalls +" Active sessions");
        return activeCalls>0;
    }
}