I have written a small spring-boot application without embedded servers. It is intended to run from command line and stay running until the VM gets signalled. What is the intended way of in the spring-boot framework (v2.0) to keep the application alive as a service? Should I have a Thread.currentThread().wait();
as last statement in my run(ApplicationArguments args)
method? Is there an enabling annotation?
Asked
Active
Viewed 3,080 times
6

Kai
- 2,145
- 1
- 20
- 35
-
1Possible duplicate of [How to prevent Spring Boot daemon/server application from closing/shutting down immediately?](https://stackoverflow.com/questions/28017784/how-to-prevent-spring-boot-daemon-server-application-from-closing-shutting-down) – Arnaud Jul 17 '18 at 09:24
-
it indeed looks duplicate, but not conclusive, and likely outdated. Most hints look very hacky. – Kai Jul 17 '18 at 09:52
-
`System.in.read` would block until the first key stroke... I want the thing just wait for Crtl-C. – Kai Jul 17 '18 at 09:53
-
@Kai the solultion given in this answer seems the right approach https://stackoverflow.com/a/28020806/1976843 better than waiting for a key stroke – JEY Jul 17 '18 at 12:05
-
@JEY That approach is bad as it will also work on all SpringBootTest tests. Hence those tests won't stop. – Sven Döring May 25 '20 at 11:45
-
The solution with adding an empty scheduled task seems the least hacky one. @Scheduled(fixedDelay = Long.MAX_VALUE) public void doNotShutdown() {} It even works with the most basic Spring Boot Starter. – Sven Döring May 25 '20 at 11:51
-
@SvenDöring you are right about that but if you are doing Unit Test without SpringBootTest this work. Enabling Scheduling is an idea. I didn't check but maybe since 2018 they are new solutions. – JEY May 26 '20 at 07:43
1 Answers
-1
from org.springframework.boot.web.embedded.netty.NettyWebServer
, Official.
private void startDaemonAwaitThread(DisposableServer disposableServer) {
Thread awaitThread = new Thread("server") {
@Override
public void run() {
disposableServer.onDispose().block();
}
};
awaitThread.setContextClassLoader(getClass().getClassLoader());
awaitThread.setDaemon(false);
awaitThread.start();
}

Yue Gu
- 61
- 6
-
that kind of contradicts the question condition of having a Spring-Boot application without a webserver, doesn't it? – Kai May 27 '20 at 02:22
-
I just assume there is another server instead of default web server, so it is necessary to keep application running. Refer to the official implementation, you can start a thread which blocked and waiting your server close. – Yue Gu May 28 '20 at 08:09