由于jsp编译,Spring启动应用程序运行缓慢

时间:2015-04-15 14:42:56

标签: spring-boot

我正在使用spring boot 1.2.2。它是一个使用jsp的标准Web应用程序并作为可执行的战争运行:Java -jar myapp.war。

当我在桌面上运行此应用程序时,一切都很好。 但是,在我们使用的服务器(Debian)上它真的很慢。

我收集了一些线程转储,我看到了一些奇怪的东西。似乎应用程序在编译jsp时遇到了一些麻烦(参见下面的stacktraces)。

似乎jsp总是被重新编译。 有谁知道会出现什么问题?

"http-nio-8443-exec-8" #50 daemon prio=5 os_prio=0 tid=0x000000000288f800 nid=0x7452 runnable [0x00007fd611adc000]
java.lang.Thread.State: RUNNABLE
at java.lang.Throwable.fillInStackTrace(Native Method)
at java.lang.Throwable.fillInStackTrace(Throwable.java:783)
- locked <0x00000000ec2eb250> (a java.io.FileNotFoundException)
at java.lang.Throwable.<init>(Throwable.java:265)
at java.lang.Exception.<init>(Exception.java:66)
at java.io.IOException.<init>(IOException.java:58)
at java.io.FileNotFoundException.<init>(FileNotFoundException.java:64)
at org.springframework.boot.loader.jar.JarURLConnection.throwFileNotFound(JarURLConnection.java:118)
at org.springframework.boot.loader.jar.JarURLConnection.connect(JarURLConnection.java:107)
at org.springframework.boot.loader.jar.JarURLConnection.getInputStream(JarURLConnection.java:175)
at sun.misc.URLClassPath$Loader.findResource(URLClassPath.java:516)
at sun.misc.URLClassPath.findResource(URLClassPath.java:176)
at java.net.URLClassLoader$2.run(URLClassLoader.java:557)
at java.net.URLClassLoader$2.run(URLClassLoader.java:555)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:554)
at org.springframework.boot.loader.LaunchedURLClassLoader.findResource(LaunchedURLClassLoader.java:78)
at org.springframework.boot.loader.LaunchedURLClassLoader.getResource(LaunchedURLClassLoader.java:69)
at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:232)
at org.apache.catalina.loader.WebappClassLoaderBase.getResourceAsStream(WebappClassLoaderBase.java:1096)
at org.apache.jasper.servlet.JasperLoader.getResourceAsStream(JasperLoader.java:142)
at org.apache.jasper.compiler.JDTCompiler$1.findType(JDTCompiler.java:197)
at org.apache.jasper.compiler.JDTCompiler$1.findType(JDTCompiler.java:182)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:158)
at org.eclipse.jdt.internal.compiler.lookup.PackageBinding.getType(PackageBinding.java:145)
at org.eclipse.jdt.internal.compiler.lookup.Scope.findType(Scope.java:2038)
at org.eclipse.jdt.internal.compiler.lookup.Scope.getTypeOrPackage(Scope.java:3350)
at org.eclipse.jdt.internal.compiler.lookup.Scope.getBinding(Scope.java:2304)
at org.eclipse.jdt.internal.compiler.ast.SingleNameReference.resolveType(SingleNameReference.java:979)
at org.eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(MessageSend.java:602)
at org.eclipse.jdt.internal.compiler.ast.MessageSend.resolveType(MessageSend.java:648)
at org.eclipse.jdt.internal.compiler.ast.Expression.resolve(Expression.java:1020)
at org.eclipse.jdt.internal.compiler.ast.Block.resolve(Block.java:119)
at org.eclipse.jdt.internal.compiler.ast.Initializer.resolve(Initializer.java:121)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1143)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1320)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.java:587)
at org.eclipse.jdt.internal.compiler.Compiler.process(Compiler.java:770)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:470)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:440)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:570)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:356)
- locked <0x00000000c6648440> (a org.apache.jasper.servlet.JspServletWrapper)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)

1 个答案:

答案 0 :(得分:5)

Tomcat的JspServlet正在开发模式下运行。有关详细信息,请参阅this issue

您可以通过将以下bean添加到您的应用程序来解决此问题:

@Bean
public EmbeddedServletContainerCustomizer servletContainerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {

        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                customizeTomcat((TomcatEmbeddedServletContainerFactory)container); 
            }
        }

        private void customizeTomcat(TomcatEmbeddedServletContainerFactory tomcatFactory) {
            tomcatFactory.addContextCustomizers(new TomcatContextCustomizer() {

                @Override
                public void customize(Context context) {
                    Container jsp = context.findChild("jsp");
                    if (jsp instanceof Wrapper) {
                        ((Wrapper)jsp).addInitParameter("development", "false");
                    }

                }

            });
        }

    };
}