RocketBoom
RocketBoom

Reputation: 139

Where the embedded Tomcat extracts?

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?

Upvotes: 1

Views: 1803

Answers (1)

alexbt
alexbt

Reputation: 17045

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 ?

Upvotes: 4

Related Questions