我使用Java编程语言和SprongBoot。我在收到 SIGTERM 命令时实现了应用程序的正确终止。
设置 SpringBoot 的设置:
`服务器: 关机:优雅
spring.task.执行: 水池: 允许核心线程超时:true 保持存活:3m 关闭: 等待终止: true 等待终止时间:3m`
我也用 ThreadPoolTaskExecutor 尝试过:
ThreadPoolTaskExecutor defaultExecutor = new ThreadPoolTaskExecutor(); defaultExecutor.setKeepAliveSeconds(180); defaultExecutor.setWaitForTasksToCompleteOnShutdown(true); defaultExecutor.setAwaitTerminationSeconds(180);
但是我遇到了这样的问题。在异步线程中,调用 SOAP 方法并因错误而崩溃:
ERROR [iso20022.app.FCR,3b58933b54e5e47c,1eb7da1d0650e9d9] 2808 --- [ h2h-async1] r.a.h.c.RepeaterInvocationHandler : error on calling jdk.proxy6.$Proxy236.wsAccountBaseInfoGet java.lang.ExceptionInInitializerError: null at com.sun.xml.ws.transport.http.client.HttpTransportPipe.getTransport(HttpTransportPipe.java:154) ~[jaxws-rt-2.3.1.jar:2.3.1] … Caused by: java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.xml.bind.JAXBContext]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1432) ~[tomcat-embed-core-9.0.76.jar:9.0.76] at org.apache.catalina.loader.WebappClassLoaderBase.getResourceAsStream(WebappClassLoaderBase.java:1140) ~[tomcat-embed-core-9.0.76.jar:9.0.76] at javax.xml.bind.ContextFinder.firstByServiceLoaderDeprecated(ContextFinder.java:611) ~[jaxb-api-2.4.0-b180830.0359.jar:2.3.0]
请帮忙。我需要做什么?
错误消息表明 SOAP 客户端正在抛出异常,因为它正在尝试访问因 Web 应用程序停止而不可用的资源 (
META-INF/services/javax.xml.bind.JAXBContext
)。
当应用程序关闭且 SOAP 客户端仍在尝试发出请求时,可能会出现此问题。为了优雅地处理这个问题,您可以捕获异常并进行适当的处理。以下是如何执行此操作的示例:
import javax.xml.ws.WebServiceException;
// ...
try {
// Call the SOAP method
// Your SOAP method invocation code here
} catch (WebServiceException ex) {
if (ex.getCause() instanceof IllegalStateException) {
IllegalStateException illegalStateException = (IllegalStateException) ex.getCause();
if (illegalStateException.getMessage().startsWith("Illegal access: this web application instance has been stopped")) {
// Handle the case where the web application is stopping
// You can choose to log the error or perform any necessary cleanup
// Example: logger.error("SOAP client error: Web application is stopping", ex);
} else {
// Handle other WebServiceException cases
}
} else {
// Handle other WebServiceException cases
}
}
在上面的代码中,我们捕获
WebServiceException
并检查异常原因是否是 IllegalStateException
以及指示 Web 应用程序正在停止的特定错误消息。如果匹配,您可以相应地处理情况(例如,记录错误消息或执行清理操作)。
通过处理异常,可以防止在关闭过程中调用 SOAP 客户端时应用程序崩溃。