Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring - Jetty startup delay due to scanning

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
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...