由于扫描导致 Jetty 启动延迟 [英] Jetty startup delay due to scanning

查看:33
本文介绍了由于扫描导致 Jetty 启动延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文和设置信息:

  • Jetty 9 和 Eclipse 码头插件
  • 带有 Spring Security 3.2.3 的 Spring 4.1.1.RELEASE
  • Spring Java 配置(无 web.xml)

问题描述在使用 Spring 的 JavaConfig 来引导 Spring 上下文而不是使用 web.xml 的项目中,启动 jetty 9 非常慢.Jetty 在相当长的一段时间内似乎什么也没做.这发生在该行之后:

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 确实会最终启动,但与常规的 tomcat 7 发行版相比,启动需要很长时间.

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

其他资源

    public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    
    //implementation
    }

推荐答案

这是因为 Jetty 9 扫描 WEB-INF 文件夹中的所有 Jars 以获取注释以启动 Web 上下文.如果您试图找到此问题的解决方案,您可能已经发现了这一事实.我尝试了几个这样的答案,但从未在其中找到正确的解决方案.

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.

为了尽可能地消除这种扫描,我们可以定义一个模式,告诉 Jetty 哪些源要扫描,哪些不扫描.这是通过在 maven 中设置一些配置,或者通过在 jetty-context.xml 中设置一个属性来完成的.(如果你使用的是maven插件,你需要在你的pom.xml中也设置Jetty的jetty-context.xml)

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 启动延迟带有 maven 插件的 jetty8 需要很长时间才能启动

正确的解决方案也是使用这样的 jetty-context.xml 完成的,但使用另一种模式.在 Spring 应用程序中,我们需要扫描 Spring jar,如果您有很多依赖项,仅此一项就已经可以带来巨大的提升.更好的是,如果您只扫描 spring-web 罐子.如果您有 Spring Security,那么可能还需要包含这些 jar.

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>

我们排除了任何不在 WEB-INF 中的 classes 文件夹中的内容,以及任何不包含给定正则表达式模式的 jar.

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!

这篇关于由于扫描导致 Jetty 启动延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆