问题
I have a spring boot 2.3+ application with server.shutdown=graceful which, when getting shut down throws:
2020-11-30 11:07:35.485 WARN 3038 --- [SpringContextShutdownHook] o.s.c.support.DefaultLifecycleProcessor : Failed to stop bean 'webServerGracefulShutdown'
java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/springframework/boot/web/server/GracefulShutdownResult
at org.springframework.boot.web.servlet.context.WebServerGracefulShutdownLifecycle.stop(WebServerGracefulShutdownLifecycle.java:51)
at org.springframework.context.support.DefaultLifecycleProcessor.doStop(DefaultLifecycleProcessor.java:238)
at org.springframework.context.support.DefaultLifecycleProcessor.access$300(DefaultLifecycleProcessor.java:53)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(DefaultLifecycleProcessor.java:377)
at org.springframework.context.support.DefaultLifecycleProcessor.stopBeans(DefaultLifecycleProcessor.java:210)
at org.springframework.context.support.DefaultLifecycleProcessor.onClose(DefaultLifecycleProcessor.java:128)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1022)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.doClose(ServletWebServerApplicationContext.java:170)
at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:949)
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/web/server/GracefulShutdownResult
... 9 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.web.server.GracefulShutdownResult
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 common frames omitted
2020-11-30 11:08:05.486 INFO 3038 --- [SpringContextShutdownHook] o.s.c.support.DefaultLifecycleProcessor : Failed to shut down 1 bean with phase value 2147483647 within timeout of 30000ms: [webServerGracefulShutdown]
2020-11-30 11:08:35.514 INFO 3038 --- [SpringContextShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-11-30 11:08:35.519 INFO 3038 --- [SpringContextShutdownHook] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler'
2020-11-30 11:08:35.520 INFO 3038 --- [SpringContextShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-11-30 11:08:35.528 INFO 3038 --- [SpringContextShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2020-11-30 11:08:35.530 INFO 3038 --- [SpringContextShutdownHook] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService
In the end the application is shutdown, however not gracefully, and since the shutdown is being called from a deployment script, the script fails, due to the exit code != 0 of the app's shutdown call.
What could be the cause of this? I think I don't have long running tasks, which can cause this, but how can I check it?
回答1:
This problem might happen if you replace or rename the jar while the jar is still running. Java does not take the whole jar in memory at startup and uses reference for classes in the Jar when it needs to load some classes. But renaming the jar or replacing the jar can mess up the class references inside the Jar. So java will not find the class you want to load at runtime and you will be presented with this ClassNotFoundException
.
Possible workaround:
Try to shutdown the application before replacing or renaming the jar.
来源:https://stackoverflow.com/questions/65090165/graceful-shutdown-fails