Jetty startup delay due to scanning

戏子无情 提交于 2019-11-28 03:09:45

问题


Context and setup information:

  • Jetty 9 with the eclipse jetty plugin
  • Spring 4.1.1.RELEASE with Spring Security 3.2.3
  • Spring Java Configuration (no web.xml)

Problem description Starting jetty 9 is very slow in a project where Spring's JavaConfig is used to boot the Spring context instead of using a web.xml. Jetty seems to be doing nothing during a considerate amount of time. This occurs after the line:

INFO:oejs.Server:main: jetty-9.2.3.v20140905

Jetty does start eventually, but takes very long to start up compared to a regular tomcat 7 distribution.


Additional resources

    public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    
    //implementation
    }

回答1:


This is due to the fact that Jetty 9 scans all Jars in the WEB-INF folder for annotations in order to start the web context. If you've attempted to find a solution to this problem, you probably already discovered that fact. I tried several such answers, but never found the correct solution among them.

In order to eliminate such scanning as much as possible, we can define a pattern that tells Jetty which sources to scan and which not to scan. This is done by either setting some configuration in maven, or by setting an attribute in the jetty-context.xml. (If you are using the maven plugin, you need to set Jetty's jetty-context.xml also in your pom.xml)

Some other solutions that have not worked for me (either no increase in startup time or no correct startup at all)

Jetty 8.1.2 startup delay jetty8 with maven plugin takes to long to start

etc.

The correct solution is also done using such jetty-context.xml, but with another pattern. In a Spring application, we need to scan the Spring jars, and this alone will already give a massive boost if you have many dependencies. Even better is if you only scan the spring-web jars instead. If you have Spring Security, then it might also be needed to include those jars.

As such, the pattern that gave me maximum speedup is shown here:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <Call name="setAttribute">
        <Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg>
        <Arg>.*/spring-security[^/]*\.jar$|.*/spring-web[^/]*\.jar$|.*/classes/.*</Arg>
    </Call>

</Configure>

We exclude anything that is not in our classes folder in WEB-INF, as well as any jars that do not include the regex pattern given.

Hope this helps someone!



来源:https://stackoverflow.com/questions/26927939/jetty-startup-delay-due-to-scanning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!