【发布时间】:2019-06-17 21:58:09
【问题描述】:
我正在使用来自here 的代码重新启动我的 Spring Boot 应用程序:
Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();
但是,重新启动的应用程序无法创建 bean,最终是由于在尝试调度 @Scheduled-annotated 方法时抛出了 RejectedExecutionException:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBean' defined in URL [insert-path-here]: Initializetion of bean failed; nested exception is org.springframework.core.TaskRejectedException: Executor [java.util.concurrent.ScheduledThreadPoolExecutor@284a4a89[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 21508]] did not accept task: my.package.MyBean.scheduledMethod
令人尴尬的是,事实证明 doRestart() 调用之后的日志表明 ExecutorService taskScheduler 正在关闭,这可以解释为什么会发生这种情况。我不完全确定的是为什么没有重新创建 ExecutorService。taskScheduler 是我的应用程序定义的 bean 并且没有注释 @RefreshScope。现在我得到的错误是
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'anotherBean' definen in URL[...]: Unsatisfied dependency expressed through constructor parameter 0 ... nested exception is java.lang.IllegalStateException: org.springframework.boot.servlet.context.AnnotationConfigServletWebServerApplicationContext@2a95076c has been closed already
在日志中向上滚动一点显示有一个 ApplicationContextException 抛出,堆栈跟踪包含 doRestart:
org.springframework.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Filters cannot be added to context [/myapp] as the context has been initialized
at org.springframework.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)
...
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.cloud.context.restart.RestartEndpoint(RestartEndpoint.java:160)
at my.package.MyBean.lambda$doThingAndRestart$2 (MyBean.java: 118)
谷歌搜索此错误似乎导致single page 不相关,尽管我确实注意到此应用程序也在 tomcat 上运行。
我怀疑这与应用程序创建了错误类型的应用程序上下文有关,即AnnotationConfigServletWebServerApplicationContext,它没有扩展AbstractRefreshableAplicationContext。
我需要做些什么来消除这个错误?
当我只从堆栈跟踪中复制单行时,请多多包涵。我在两台计算机上设置,所以我不能复制和粘贴,也不允许将日志移动到连接到 Internet 的计算机上。
【问题讨论】:
标签: java spring-boot tomcat