What is embedded Tomcat?
An embedded Tomcat server consists of a single Java web application along with a full Tomcat server distribution, packaged together and compressed into a single JAR, WAR or ZIP file.
Embedded Tomcat offers a way to package Java web applications that is consistent with a microservices-based approach to software development. It also makes it easier to distribute Java web applications through Docker containers and manage them through a container orchestration service, such as Kubernetes or OpenShift.
Embedded Tomcat configuration
In a traditional setup, an enterprise would use a single, standalone Tomcat server and deploy all Java web applications to that one instance.
That one application server would then be clustered and scaled both horizontally and vertically to manage throughput and capacity. With this traditional configuration, the cardinality between the Tomcat server and the applications deployed to it is always 1-to-many.
Standalone Tomcat servers
With an embedded Tomcat server, the ratio between the server and the application is 1-to-1. A single Java web application is deployed to a single Tomcat server. All the files associated with both the Tomcat server and deployed application are compressed into a single archive file, typically with a .zip, .jar or .war extension.
The basis of the term embedded Tomcat refers to the fact that the application and the entire Tomcat server is packaged within a single, easily managed and compressed archive file.
Embedded Tomcat example
Another key element of an embedded Tomcat server is the executable file. As long as a computer has the JRE installed and JAVA_HOME configured, you can run an embedded Tomcat server from the command line when you provide the name of the JAR file to the java -jar utility.
Here is the command required to run an embedded Tomcat application packaged in a file named embeded-tomcat-example.jar:
C:/> java -jar embedded-tomcat-example.jar
When the command completes, the Tomcat server starts and the Java web application deployed within is accessible through an HTTP request, such as a web page, SOAP-based web service or a RESTful API call.
The default embedded Tomcat port is 8080.
Embedded Tomcat and Maven
One of the most common ways to create an embedded Tomcat file is to use the Maven-Tomcat plugin for a build.
With this approach, a developer creates Maven-based Java web projects without any regard to the manner in which the application will be packaged. Then, during the Maven build, a reference is made to the exec-war-only switch of the Tomcat plugin.
Here is an example of how to create an executable, embedded Tomcat JAR file with the Maven-Tomcat plugin.
/c/ embedded Tomcat example/maven-build
$ mvn clean install tomcat7:exec-war-only
[INFO] --- embedded tomcat maven plugin running
[INFO] MAVEN BUILD SUCCESS : EMBEDDED TOMCAT JAR CREATED
[INFO] Elapsed time: 2.211 s
Embedded Tomcat and Spring Boot
Spring Boot is a popular Java-based framework to develop microservices. By default, the Spring Tool Suite (STS) IDE -- which is used to build a Spring Boot application -- will automatically create an embedded Tomcat server with the microservices developed each time a build or deployment occurs.
Additional features are built into STS to facilitate microservices-based development, including the ability to set breakpoints and subsequently step through code as it executes on the embedded Tomcat server.
Embedded Tomcat alternatives
Tomcat competes with other Java application servers, including WebSphere, Jetty, Wildfly, Payara and JBoss. Each of these servers provides facilities to create a single JAR or WAR file in which the server and the application are embedded.
IBM clients who move away from WebSphere Portal instances and towards a microservices architecture can follow a common migration path with an embedded WebSphere Liberty instance.
Jetty is the embedded server used in standalone distributions of Jenkins CI. Wildfly is highly modular and can be stripped of any unneeded services. It is often a popular alternative to an embedded Tomcat instance when you want a server with a minimal footprint.
Embedded Tomcat vs. standalone
As each embedded Tomcat instance runs its own, full-blown instance of the Tomcat server, upfront performance costs are high when compared to a standalone version.
For example, if an organization ran 10 embedded Tomcat instances, 10 separate Tomcat server instances are started. A standalone instance, however, uses 10 applications deployed to only one instance.
In this scenario, standalone performance would be much faster because only one Java process would need to be started. All incoming requests could be handled by efficient threads.
With an embedded Tomcat, each server requires its own Java process and additional threads handle incoming requests. A Java process requires more processing power than a single thread of execution. As a result, an embedded Tomcat server requires more resources to run when compared to a standalone server that hosts multiple applications
Benefits of embedded Tomcat
The aforementioned scenario that compares embedded Tomcat performance to standalone instances misses the point of why a developer would deploy applications to an embedded Tomcat instance. With an embedded Tomcat instance, individual applications can be taken offline or restarted without affecting others. This isn't possible with a standalone architecture.
Furthermore, if a single application requires more resources, it can be workload managed. With a traditional implementation, a single Tomcat server must be workload managed, which means every hosted application needs to be scaled even if only one of the deployed apps experienced an elevated load.
Tomcat, Docker and Kubernetes
The main idea behind the delivery of a Java web application within an executable, embedded Tomcat instance is the ability to move that file into a Docker container and deploy the container into a container-hosted environment such as Kubernetes or OpenShift. This feature opens a variety of low-cost, cloud-based scaling options that are not available to standalone Tomcat instances.
Hence, the use of an embedded Tomcat server is key to allow traditionally developed Java web applications to take advantage of modern, enterprise architectures.