Embedding tomcat server into an application
What is an embedded server?
A web application runs on a web server, usually they are packaged in a WAR file and deployed in a server container like Tomcat, JBOSS or GlassFish. However embedded servers provide an interesting alternative. Instead of deploying your app in a server, the server is embedded inside your application The result is a standalone application packaged in a jar file and that could be executed from a command line.To run an embedded tomcat server we just need following jars
tomcat-embed-coretomcat-embed-logging-juli
Find this project on Github
https://github.com/javacodenet/embeddedTomcatDemopom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.javacodenet</groupId> | |
<artifactId>embedded-tomcat-demo</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-core</artifactId> | |
<version>8.0.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat.embed</groupId> | |
<artifactId>tomcat-embed-logging-juli</artifactId> | |
<version>8.0.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
EmbeddedTomcatStarter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.catalina.Context; | |
import org.apache.catalina.LifecycleException; | |
import org.apache.catalina.startup.Tomcat; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.*; | |
import java.util.Properties; | |
public class EmbeddedTomcatStarter { | |
public static void main(String[] args) | |
throws LifecycleException, InterruptedException, ServletException { | |
Tomcat tomcat = new Tomcat(); | |
int port = Integer.parseInt(getPort()); | |
tomcat.setPort(port); | |
Context context = tomcat.addContext("/", new File(".").getAbsolutePath()); | |
addServletToTomcat(port, context); | |
context.addServletMapping("/*", "embeddedTomcat"); | |
tomcat.start(); | |
tomcat.getServer().await(); | |
} | |
private static String getPort() { | |
String port = null; | |
InputStream input = null; | |
try { | |
Properties properties = new Properties(); | |
input = new FileInputStream("src/main/resources/application.properties"); | |
properties.load(input); | |
//read server.port from application properties | |
port = properties.getProperty("server.port"); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} finally { | |
if (input != null) { | |
try { | |
input.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
//if port is not configured then it will defaulted to 8080 | |
return port != null ? port : "8080"; | |
} | |
private static void addServletToTomcat(final int port, Context context) { | |
Tomcat.addServlet(context, "embeddedTomcat", new HttpServlet() { | |
protected void service(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
Writer writer = response.getWriter(); | |
writer.write("Embedded Tomcat Server is Running at:" + port); | |
writer.flush(); | |
} | |
}); | |
} | |
} |
application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.port = 8585 |
Comments
Post a Comment