问题

我们有一个运行25个Web应用程序的Tomcat 7.0.35,它们都共享具有相同版本的相同库(保证不会改变!)。我认为不是每个项目都加载相同的库,而是可以将它们移动到${catalina.home}/lib,从而减少所需的内存负载。

但这是有代价的:每个Web应用程序的部署时间从5秒增加到2分钟,这是不可行的!

我读了一些书(例如Class Loader HOW-TO),不幸的是我找不到减少启动时间的方法。有办法解决这个问题吗?

请求的概要信息

我在两种不同的情况下运行了jstackjvisualvm,我将在此处介绍一些发现(请告诉我您是否需要其他信息):

  • 每个项目专用的库似乎已加载:org.apache.catalina.loader.WebappClassLoader.findResourceInternal运行该方法的线程的jstack输出:
    "localhost-startStop-1" daemon prio=10 tid=0x00007f17f8001800 nid=0x13b8 runnable [0x00007f183c800000]
       java.lang.Thread.State: RUNNABLE
            at java.text.MessageFormat.subformat(MessageFormat.java:1250)
            at java.text.MessageFormat.format(MessageFormat.java:819)
            at org.apache.naming.StringManager.getString(StringManager.java:145)
            at org.apache.naming.resources.BaseDirContext.lookup(BaseDirContext.java:500)
            at org.apache.naming.resources.ProxyDirContext.lookup(ProxyDirContext.java:310)
            at org.apache.catalina.loader.WebappClassLoader.findResourceInternal(WebappClassLoader.java:3011)
            at org.apache.catalina.loader.WebappClassLoader.findResource(WebappClassLoader.java:1262)
            at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream(WebappClassLoader.java:1499)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2302)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2294)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2308)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2291)
            at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2197)
            at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2154)
            at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2034)
            at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1990)
            at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1976)
            at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1326)
            at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:882)
            - locked <0x00000007f52f3ec0> (a org.apache.catalina.startup.ContextConfig)
            at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:379)
            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:5343)
            - locked <0x00000007f52f3bb8> (a org.apache.catalina.core.StandardContext)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
            - locked <0x00000007f52f3bb8> (a org.apache.catalina.core.StandardContext)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:902)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:879)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:636)
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1236)
            at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1877)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:745)
    
       Locked ownable synchronizers:
            - <0x00000007846dfc70> (a java.util.concurrent.ThreadPoolExecutor$Worker)
    

  • 项目之间共享的库似乎已加载:at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream导致难以置信的部署时间。在项目部署期间,JVisualVM会为此方法显示98%+的“自用时间”。
    运行该方法的线程的jstack输出:
    "localhost-startStop-1" daemon prio=10 tid=0x00007f6280001000 nid=0x6ba3 runnable [0x00007f62c4950000]
       java.lang.Thread.State: RUNNABLE
            at java.lang.String.intern(Native Method)
            at java.util.jar.Attributes$Name.<init>(Attributes.java:466)
            at java.util.jar.Attributes.putValue(Attributes.java:168)
            at java.util.jar.Attributes.read(Attributes.java:421)
            at java.util.jar.Manifest.read(Manifest.java:251)
            at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:252)
            at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:239)
            at java.util.jar.JarVerifier.processEntry(JarVerifier.java:307)
            at java.util.jar.JarVerifier.update(JarVerifier.java:218)
            at java.util.jar.JarFile.initializeVerifier(JarFile.java:345)
            at java.util.jar.JarFile.getInputStream(JarFile.java:412)
            - locked <0x00000007e8e9d890> (a sun.net.www.protocol.jar.URLJarFile)
            at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:162)
            at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:233)
            at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream(WebappClassLoader.java:1528)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2302)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2294)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2308)
            at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2294)
            at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2197)
            at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2154)
            at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2034)
            at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1990)
            at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1976)
            at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1326)
            at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:882)
            - locked <0x00000007858db6e0> (a org.apache.catalina.startup.ContextConfig)
            at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:379)
            at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:118)
            at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:91)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5346)
            - locked <0x00000007858db318> (a org.apache.catalina.core.StandardContext)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
            - locked <0x00000007858db318> (a org.apache.catalina.core.StandardContext)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:902)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:879)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:636)
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1236)
            at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1877)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:745)
    
       Locked ownable synchronizers:
            - <0x000000078460e618> (a java.util.concurrent.ThreadPoolExecutor$Worker)
    
  • 最佳答案

    WebappClassLoader首先搜索自己的类路径。因此,对于不在本地类路径中的每个类,将查看所有jar文件。只有这样,它才会查看公共(public)类加载器的委托(delegate)。

    假设调查所有 war 的jar需要花费x的时间,对于jars的父类加载器来说,则需要y的时间。

    通常,从正确的类加载器加载类将花费一半的时间来扫描它们。

    从WebappClassLoader加载类将花费x/2的时间。

    从父类加载器加载类将花费x + y/2。

    根据x和y,您可以更改使用类加载器的顺序。
    如果y大于x,则应保留默认设置;如果x大于x,则可以尝试反转查找顺序。如果这样做,从公共(public)类加载器加载类将花费y/2的时间,而从WebappClassLoader加载类将花费y + x/2。

    WebbappClassLoader具有一个委托(delegate)属性,将此属性设置为true应该可以解决问题。
    参见Common Attributes

    07-24 20:36