Jetty startup delay due to scanning
Asked Answered
P

1

10

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
    }
Palacios answered 14/11, 2014 at 10:31 Comment(0)
P
20

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!

Palacios answered 14/11, 2014 at 10:31 Comment(5)
There's also the quickstart module in Jetty 9.2.x that can do the scan at build/compile time, which creates a jetty-quickstart.xml of what it finds in the war file, making the production startup sub-second.Deron
That's pretty cool actually. I'll try to find an example of how to do that, so I can add it to this answer, or you can submit your own if you have one in an answer :). Thanks!Palacios
I don't see any big difference regardless which solution I tried.Portulaca
Are you sure the jetty-context is being used? If so, try tweaking the regular expression, there might be other big libraries that cause the delay.Palacios
If using Gradle instead of Maven, I could successfully apply this configuration using the Gretty plugin and setting the jetty-env configuration parameter .Goldiegoldilocks

© 2022 - 2024 — McMap. All rights reserved.