1

Where the embedded in executable jar Tomcat archive extracts?

In memory or in the some folder on hard drive?

How many resources it requires compared with ordinary Tomcat?

Clifford
  • 88,407
  • 13
  • 85
  • 165
RocketBoom
  • 139
  • 3
  • 10

1 Answers1

4

This probably won't be a complete answer, but just as much as I can explain. I too was wondering, so I did a little digging.

tomcat-embed-core

With spring boot, we usually include spring-boot-starter-tomcat (through spring-boot-starter-web), which includes tomcat-embed-core.

If you were to create a normal main method, and add tomcat-embed-core-x.y.z.jar to your classpath, you could simply do this (source: http://people.apache.org/~markt/presentations/2010-11-04-Embedding-Tomcat.pdf):

Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);

// Create a context
File docBase = new File(System.getProperty("java.io.tmpdir"));
Context ctxt = tomcat.addContext("",docBase.getAbsolutePath());

// Add a Servlet
Tomcat.addServlet(ctxt, "HelloWorld", new HelloWorldServlet());
ctxt.addServletMapping("/*", "HelloWorld");

// Start the instance
tomcat.start();
// Loop to serve requests
while(true) {
    Thread.sleep(5000);
}

So, when spring-boot-maven-plugin packages the uber-jar, it includes tomcat-embed-core within the jar.

Autoconfiguration - new Tomcat()

Spring Boot starts its feature through autoconfiguration, one of them is the EmbeddedServletContainerAutoConfiguration:

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class)
public class EmbeddedServletContainerAutoConfiguration {
...
@Configuration
    @ConditionalOnClass({ Servlet.class, Tomcat.class })
    @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
    public static class EmbeddedTomcat {

        @Bean
        public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
            return new TomcatEmbeddedServletContainerFactory();
        }
    }

If tomcat and Servlet are found on the classpath, TomcatEmbeddedServletContainerFactory will be instantiated, and it all starts..

Somewhere in spring-boot (EmbeddedWebApplicationContext.refresh) a Tomcat instance is created by calling TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(), which does a new Tomcat();

From this point on

From this point, I can relate with the simple main method, doing a simple new Tomcat(). For me, that's what Spring Boot does and then registers all the Servlets. The rest is good old spring. Obviously, Spring Boot offers tons of AutoConfiguration and properties to configure just about everything!

Does that help a little, of perhaps you knew this and were hoping for more ?

alexbt
  • 16,415
  • 6
  • 78
  • 87
  • 1
    Thanks, Alex, for your reply. You certainly helped, but I think I was not enough accurate in my question. I mean, "in which place the embedded Tomcat extracts?". Ordinary Tomcat archive takes ~7-9mb, and 70-80 after it extracts, on hdd. And it's not clear to me, in which place the embedded Tomcat extracts from point of the resources used. From your example, the embedded Tomcat uses "java.io.tmpdir" for it's context, but this dir takes very little disk space(~16kb) after the app starts. – RocketBoom Sep 08 '16 at 08:51