当我关闭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上下文重新加载时的行为,我认为这应该是开箱即用的。
在继续servlet上下文重新加载过程之前,有没有办法让WebApplicationContext正常关闭?
我在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()
行。似乎在下游发生了一些错误。可能会吞下一些例外。
根据要求,这是我配置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()
是一种配置实例名称,组名称和密码,地图名称和地图索引的方法,所以我认为这不值得关注。
这是我的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;
}
答案 0 :(得分:7)
在某些时候,您提到Logback存在NoClassDefFoundError
。你通过删除这个依赖关系来解决这个问题,然后将问题转移到另一个类 - 一个Spring自己的类。
这可能意味着您所拥有的任何一个库都会对类加载器造成错误,或者Tomcat可能需要不对某些资源进行锁定的指令。请参阅here有关被锁定的Tomcat资源和<Context>
设置的更多信息:在Tomcat的conf/context.xml
antiResourceLocking="true"
中添加{{1}}元素。
答案 1 :(得分:5)
您是否尝试过为Tomcat上下文提升unloadDelay
(默认为2000毫秒)?见http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
UPDATE :我发现你也遇到了回退问题,尝试注册这个监听器也许值得一试:
class LogbackShutdownListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
System.out.println("Shutting down Logback context '" + loggerContext.getName() + "' for " + contextRootFor(event));
loggerContext.stop();
}
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("Logback context shutdown listener registered for " + contextRootFor(event));
}
private String contextRootFor(ServletContextEvent event) {
return event.getServletContext().getContextPath();
}
}
请务必在弹簧上下文加载器侦听器之前声明此侦听器,以便在关闭时上下文侦听器后调用。
更新2 :也值得尝试注册另一个bean来手动关闭Hazelcast的东西(确保也从hazelcast bean中删除destroyMethod):
@Component
class HazelcastDestructor {
@Autowired
private HazelcastInstance instance;
@PreDestroy
public void shutdown() {
try {
instance.shutdown();
} catch (Exception e) {
System.out.println("Hazelcast failed to shutdown(): " + e);
throw e;
}
}
}
更新3 :出于好奇,您是否尝试过并行部署:http://www.javacodegeeks.com/2011/06/zero-downtime-deployment-and-rollback.html。 可能的行为与重新加载相同的上下文不同。至少你应该能够懒惰地取消部署旧版本,看看是否有所作为。
答案 2 :(得分:1)
当容器重启here时,悬空线程上存在类似问题。
在所有答案中,一个特别感兴趣的答案是霍华德 - 它显示了这些线程被清除的方式。
关于如何终止线程here,有一些很好的讨论和推理。
现在实现ServletContextListener并在contextDestroyed()方法中处理这些线程:
public class YourListener implements ServletContextListener{
....
@Override
public void contextDestroyed(ServletContextEvent event) {
//Call the immolate method here
}
}
在WebApplicationInitilizer中将此侦听器注册为:
ctx.addListener(new YourListener());
因此,当重新启动服务器时,会调用contextDestroyed方法,这会处理所有这些线程。
答案 3 :(得分:1)
从Web App开发的角度来看,ServletContainer
只能通知app的开始前和结束前的进程。
它正在使用ServletContextListener
。
在ServletContextListener
web.xml
<listener>
<listener-class>com.var.YourListener</listener-class>
</listener>
YourListener.java
public class YourListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
//initialization process
}
public void contextDestroyed(ServletContextEvent sce) {
//destory process
}
}
更新-XML Less
编程
@Override
public void onStartup(ServletContext ctx) throws ServletException {
ctx.addListener(new YourContextListener());
ctx.setInitParameter("spring.profiles.active", "production");
super.onStartup(ctx);
}
注释
@WebListener / @WebServletContextListener
public class YourContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
更新 - ShoutDown Hook In Spring
我从未在我的应用开发之前使用它,我们可以将shoutdownhook event
注册到Spring的AbstractApplicationContext
。
我不确定你会不会好。
AbstractApplicationContext context = ...
context.registerShutdownHook();