gpt4 book ai didi

java - WebApplicationContext 未在 Servlet 上下文重新加载时关闭

转载 作者:IT老高 更新时间:2023-10-28 13:59:19 31 4
gpt4 key购买 nike

当我关闭 Tomcat 时,我观察到 Spring WebApplicationContext 的正确关闭和清理。但是,当我重新部署基于 Spring 的 WAR(通过将新 WAR 复制到 webapps)时,不会发生正常关闭。由于所有随之而来的资源泄漏,这对我来说是个问题:

org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [] appears to have started a thread named [hz.hazelcast-swipe-instance.scheduled] but has failed to stop it. This is very likely to create a memory leak.
org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [] appears to have started a thread named [hz.hazelcast-swipe-instance.operation.thread-0] but has failed to stop it. This is very likely to create a memory leak.

...等等。我正在使用无 XML 配置,这是我的 WebApplicationInitializer:

public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override protected Class<?>[] getRootConfigClasses() {
return new Class[] { WebSecurityConfig.class, WebMvcConfig.class };
}
@Override protected Class<?>[] getServletConfigClasses() { return null; }

@Override protected String[] getServletMappings() { return new String[] { "/" }; }

@Override public void onStartup(ServletContext ctx) throws ServletException {
ctx.setInitParameter("spring.profiles.active", "production");
super.onStartup(ctx);
}
}

没有专门用于控制 servlet 上下文重新加载时的行为的配置,我认为这应该是开箱即用的。

有没有办法让 WebApplicationContext 在继续 servlet 上下文重新加载过程之前正确关闭?

我正在使用 Spring 4.0.5、Tomcat 7.0.54、Hazelcast 3.2.1、Hibernate 4.3.4.Final。

更新

我为 ContextClosedEvent 添加了一个 Spring 应用程序监听器并打印了其调用的堆栈跟踪:

    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333) [spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335) [spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:880) [spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:841) [spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:819) [spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1486) [catalina.jar:7.0.54]
at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1847) [catalina.jar:7.0.54]
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) [catalina.jar:7.0.54]
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5647) [catalina.jar:7.0.54]
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) [catalina.jar:7.0.54]
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1575) [catalina.jar:7.0.54]
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1564) [catalina.jar:7.0.54]

这表明 Spring 关闭发生在其 Servlet#destroy 方法中。这是来自 AbstractApplicationContext#close() 的相关片段:

        if (logger.isInfoEnabled()) {
logger.info("Closing " + this);
}

LiveBeansView.unregisterApplicationContext(this);

try {
// Publish shutdown event.
publishEvent(new ContextClosedEvent(this));
}
catch (Throwable ex) {
logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);
}
// Stop all Lifecycle beans, to avoid delays during individual destruction.
try {
getLifecycleProcessor().onClose();
}
catch (Throwable ex) {
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
}

// Destroy all cached singletons in the context's BeanFactory.
destroyBeans();

// Close the state of this context itself.
closeBeanFactory();

// Let subclasses do some final clean-up if they wish...
onClose();

synchronized (this.activeMonitor) {
this.active = false;
}

我从这个片段的开头看到了日志条目,并且我得到了我的 ContextClosedEvent。我还看到一个条目 DefaultLifecycleProcessor - Stopping beans in phase 2147483647,它可能来自 getLifecycleProcessor.onClose() 行。似乎在下游发生了一些错误。一些异常可能会被吞下。

更新 2

根据要求,这是我配置 Hazelcast 的方式:

@Bean(destroyMethod="shutdown") public HazelcastInstance hazelcast() {
final Config c = hzConfig();
final JoinConfig join = c.getNetworkConfig().getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().setEnabled(true);
return getOrCreateHazelcastInstance(c);
}

hzConfig() 是配置实例名、组名和密码、 map 名和 map 索引的方法,所以我觉得这里没什么意思。

这是我的 Hibernate SessionFactory 配置:

@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean b = new LocalSessionFactoryBean();
b.setDataSource(dataSource);
b.setHibernateProperties(props(
"hibernate.connection.release_mode", "on_close",
"hibernate.id.new_generator_mappings", "true",
"hibernate.hbm2ddl.auto", "update",
"hibernate.order_inserts", "true",
"hibernate.order_updates", "true",
"hibernate.max_fetch_depth", "0",
"hibernate.jdbc.fetch_size", "200",
"hibernate.jdbc.batch_size", "50",
"hibernate.jdbc.batch_versioned_data", "true",
"hibernate.jdbc.use_streams_for_binary", "true",
"hibernate.use_sql_comments", "true"
));
return b;
}

最佳答案

在某些时候,您提到有一个 NoClassDefFoundError对于 Logback。您通过删除此依赖项解决了这个问题,但随后问题转移到了另一个类 - Spring 自己的类之一。

这可能意味着您拥有的任何一个库对类加载器有问题,或者 Tomcat 可能需要指示不要保持对某些资源的锁定。见 here更多关于 Tomcat 资源被锁定和 <Context>设置尝试:在你的 Tomcat 的 conf/context.xml放置一个antiResourceLocking="true"到元素。

关于java - WebApplicationContext 未在 Servlet 上下文重新加载时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24759373/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com