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!