2

最近,我将 spring boot 版本从 升级2.3.42.4.2. 我有下面的代码来优雅地关闭应用程序。

taskScheduler.schedule(() -> SpringApplication.exit(applicationContext, () -> 0),
            Instant.now().plus(30, ChronoUnit.SECONDS));

这段代码在升级之前运行良好。现在它给出了以下异常并且应用程序继续运行(没有终止)。

2021-02-08 18:56:29.530 WARN [xyz-naming-service,,] 9040 --- [ scheduling-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Interrupted during closing
java.lang.InterruptedException: null
at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2109)
at java.base/java.util.concurrent.ThreadPoolExecutor.awaitTermination(ThreadPoolExecutor.java:1454)
at com.zaxxer.hikari.pool.HikariPool.shutdown(HikariPool.java:255)
at com.zaxxer.hikari.HikariDataSource.close(HikariDataSource.java:351)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:339)
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:273)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:587)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:559)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1152)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:520)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1145)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1111)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1080)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.doClose(ServletWebServerApplicationContext.java:171)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1026)
at org.springframework.boot.SpringApplication.close(SpringApplication.java:1369)
at org.springframework.boot.SpringApplication.exit(SpringApplication.java:1356)
at com.xyz.renaming.Migrator.lambda$migrate$1(Migrator.java:36)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

知道如何解决这个问题吗?

更新

根据@vvs 的回答,我将代码更改为首先关闭 Hikari 数据源。现在 InterruptedException 不存在。令人惊讶的是,该应用程序仍在运行。我可以看到下面的日志经常记录。意思是,这些线程没有终止。

2021-02-09 15:46:40.460 INFO [xyz-naming-service,,] 3212 --- [ool-12-thread-1] o.a.k.c.c.internals.ConsumerCoordinator : ...
4

1 回答 1

0

认为这是您的应用程序终止逻辑中的一个问题。请在 HikariCP https://github.com/brettwooldridge/HikariCP/issues/1295上查看此问题

决议说

看起来像在 SpringApplication.exit() 修复问题之前调用 hikariDataSource.close()

于 2021-02-09T07:05:56.533 回答