[Reproduced] Tomcat 7.0.3x is slow to start and the solution to the exception of StackOverflowError

之前使用tomcat7时遇到启动报错问题,日志如下:
严重: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/mypro]] 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) 
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) 
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1114) 
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1673) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
at java.lang.Thread.run(Thread.java:619) 
Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [/mypro]. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies 
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2109) 
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1981) 
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1947) 
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1932) 
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1326) 
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878) 
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369) 
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) 
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
... 11 more 
Caused by: java.lang.StackOverflowError 
at java.util.HashMap.get(HashMap.java:300) 
at org.apache.catalina.startup.ContextConfig.populateSCIsForCacheEntry(ContextConfig.java:2257) 
at org.apache.catalina.startup.ContextConfig.populateSCIsForCacheEntry(ContextConfig.java:2269) 
at org.apache.catalina.startup.ContextConfig.populateSCIsForCacheEntry(ContextConfig.java:2269) 
at org.apache.catalina.startup.ContextConfig.populateSCIsForCacheEntry(ContextConfig.java:2269) 
at org.apache.catalina.startup.ContextConfig.populateSCIsForCacheEntry(ContextConfig.java:2269) 
at org.apache.catalina.startup.ContextConfig.populateSCIsForCacheEntry(ContextConfig.java:2269) 
 …
 


After searching online for a long time, I finally solved the problem. MARK at this time. Here's a workaround: 

-------------------------------------------------------- -----The following article is reproduced----------------------------------------- ---------  

Students who use tomcat 7.0.3x can find that tomcat starts a lot slower, and may also encounter the following startup exceptions: 

Unable to complete the scan for annotations for web application [] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies. 

The tomcat7.0.3X version supports the features of servlet3.0, such as @WebServlet, @WebListener. To support these features, tomcat has to go Scan all jars for every class inside. This exception indicates that when scanning the jar package, the recursive call is too deep, causing the stack to overflow. Tomcat gave you a bad idea to increase the xss. This is still not good. The number of available threads is reduced when the xss is increased. 

分析tomcat源代码,发现它扫描的流程如下: 

1.扫描所有jar包 

2.通过查找jar包中META-INF/services/javax.servlet.ServletContainerInitializer文件内的定义,初始化ServletContainerInitializer实现 

3.如果web.xml中配置了metadata-complete="true" 或者没有找到ServletContainerInitializer实现,都不会继续扫描jar包 

网上大多数的答案的都是说在web.xml中加入了metadata-complete="true"就能避免这个异常。确实在很多场景下,这个异常是能够避免。但是使用spring-web-3.1.0.RELEASE的同学就杯具了,这个jar包中定义了一个ServletContainerInitializer,还是导致了扫描jar包。 

我们可以用另外的办法来解决这个问题,我们让tomcat不扫描指定的jar包,tomcat就要轻松得多了,org.apache.tomcat.util.scan.StandardJarScanner中定义了defaultJarsToSkip,有了这个东东,我们就可以跳过某些jar包。 

如果你不想使用servlet3.0 annotation支持,在tomcat的catalina.properties配置文件中tomcat.util.scan.DefaultJarScanner.jarsToSkip的值后面加一个",*",这样就不会扫描所有的jar包了。启动更快,也不会出异常。 

tomcat在处理扫描是还有个小bug,比如我遇到了 

SEVERE: Unable to process Jar entry [__MACOSX/cn/****/._HandlerFactory.class] from... 

这是tomcat在扫描到以.class为后缀的文件后,就分析类,很明显,此文件都不是java类文件。tomcat不应该只判断后缀为.class就是java类文件。
 


原文地址:http://qingyuexiao.iteye.com/blog/1886059

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326948791&siteId=291194637