码头因扫描而启动延迟 [英] Jetty startup delay due to scanning

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

问题描述

上下文和设置信息:

  • 带有Eclipse Jetty插件的Jetty 9
  • Spring 4.1.1.使用Spring Security 3.2.3进行发布
  • Spring Java配置(无web.xml)

问题描述 在使用Spring的JavaConfig而不是使用web.xml引导Spring上下文的项目中,启动jetty 9的速度非常慢.码头在相当长的时间内似乎无所作为.这是在以下行之后发生的:

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 jars.如果您具有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-INFclasses文件夹中不存在的所有内容,以及不包含给定正则表达式模式的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.

希望这对某人有帮助!

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

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