嵌入式Tomcat与Servlet 3.0 - 扫描时如何跳过某些罐子?

时间:2013-07-12 13:46:39

标签: java tomcat servlets

以下是扫描Servlet 3.0初始化程序的嵌入式Tomcat服务器的简单方法:

public static void main(String[] args) throws ServletException, LifecycleException {
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);

    StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("web").getAbsolutePath());

    //declare an alternate location for your "WEB-INF/classes" dir:     
    File additionWebInfClasses = new File("target/classes");
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths("/WEB-INF/classes=" + additionWebInfClasses);
    ctx.setResources(resources);

    tomcat.start();
    System.out.println("Started");
    tomcat.getServer().await();
}

我知道tomcat.util.scan.DefaultJarScanner.jarsToSkip中的属性catalina.properties允许您限制扫描哪些jar以加快部署时间。我如何将这个相同的想法融入到嵌入式Tomcat代码中?

奖励:有没有办法指定包含哪个罐子而不是哪个罐子跳过

3 个答案:

答案 0 :(得分:2)

虽然我没有特定的方法来加载单个catalina.properties属性,但我觉得在Tomcat容器上下文中重载JarScanner本身很方便,如下所示:

答案是在Groovy中(对不起,我变懒了)

public static void main(String[] args) throws ServletException, LifecycleException {
    Tomcat tomcat = new TomcatWithFastJarScanner()
    tomcat.setPort(8080)

    StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File("web").getAbsolutePath())

    //declare an alternate location for your "WEB-INF/classes" dir:     
    VirtualDirContext resources = new VirtualDirContext()
    resources.setExtraResourcePaths("/WEB-INF/classes=" + new File("target/classes"))
    ctx.setResources(resources)

    tomcat.start()
    println "Started server on port 8080"
    tomcat.getServer().await()
}

private static class TomcatWithFastJarScanner extends Tomcat {
    @Override
    public void start() throws LifecycleException {
        getServer().findServices().each { service ->
            service.getContainer().findChildren().each { container ->
                container.findChildren().each { c ->
                    ((Context) c).setJarScanner(new FastJarScanner())
                }
            }
        }
        super.start()
    }
}


private static class FastJarScanner extends StandardJarScanner {
    def jarsToInclude = [ 'spring-web.*' ]

    @Override
    public void scan(ServletContext context, ClassLoader classloader,
            JarScannerCallback callback, Set<String> jarsToSkip) {
        jarsToSkip = new HashSet<String>();

        ((URLClassLoader) classloader.getParent()).getURLs().each {
            def jar = it.path.find(/[^\/]+\.jar$/)
            if(!jar) return
            for(String inclusionPattern : jarsToInclude) {
                if(jar.find(inclusionPattern))  
                    println "including jar: " + jar
                else jarsToSkip.add(jar)
            }
        }

        super.scan(context, classloader, callback, jarsToSkip);
    }
}

基本思想是我们正在查看类加载器可以看到的所有罐子,并排除我们不想包含的所有罐子。

Tomcat嵌入式启动速度非常快!

答案 1 :(得分:0)

以下是推测性的,只是阅读规范。可以使用一个网络片段,其自己的META-INF / web.xml,其中metadata-complete = true表示没有注释扫描。< / p>

答案 2 :(得分:0)

您可以将Tomcat的scanClassPath设置为false,类似于user jkschneider here指出的那样):

new Tomcat() {
    @Override
    public void start() throws LifecycleException {
        for (final org.apache.catalina.Service service : getServer().findServices()) {
            for (final Container container : service.getContainer().findChildren()) {
                for (final Container subContainer : container.findChildren()) {
                    final StandardJarScanner jarScanner = (StandardJarScanner) ((Context) subContainer).getJarScanner();
                    jarScanner.setScanClassPath(false);
                }
            }
        }
        super.start();
    }
};

更好的方法可能如下:

final Tomcat tomcat = new Tomcat();
final StandardHost tomcatHost = (StandardHost) tomcat.getHost();
        tomcatHost.setContextClass("com.fullclassname....FastStandardContext");

FastStandardContext在哪里

public class FastStandardContext extends StandardContext {
    public FastStandardContext() {
        ((StandardJarScanner) getJarScanner()).setScanClassPath(false);
    }
}

这将涵盖在tomcat运行后添加新的Web应用程序的情况。