我们写了一个web应用,打成war包后,就需要找一个server来部署.对于我们的实际应用,我们基本没必要自己再写一个嵌入式的server.接下来两篇文章只是以钻研的心态来学习一下嵌入式tomcat和jetty. 促使我去写这篇文章是因为在我看来,gretty不完美,无论用tomcat还是jetty,修改类之后,reload几次内存占用高涨,硬盘读写频繁,最终感到无奈(应用存在内存不正确释放,或许我使用的方式不对).
我选用了<<gradle的war插件>>简单开发的web应用,此例主要开发框架:springmvc(java config方式)+spring data jpa(jpa由hibernate实现)+mysql,开发IDE工具:idea13.1,项目管理工具:gradle,源码下载:http://download.csdn.net/detail/xiejx618/7696821 一.war包配置的嵌入式tomcat例子. 1.运行一下war任务,对这个web应用打成war包.然后再解压一下这个包.
2.再新建一个gradle项目
a.build.gradle
- apply plugin: ‘java‘
- sourceCompatibility = 1.6
- version = ‘1.0‘
- repositories {
- mavenCentral()
- }
- dependencies {
- compile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.9")
- compile("org.apache.tomcat.embed:tomcat-embed-core:8.0.9")
- compile("org.apache.tomcat.embed:tomcat-embed-logging-juli:8.0.9")
- compile("org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.0.9")
- compile("org.apache.tomcat.embed:tomcat-embed-el:8.0.9")
- compile("org.apache.tomcat.embed:tomcat-embed-websocket:8.0.9")
- compile("org.eclipse.jdt.core.compiler:ecj:4.4")
- }
apply plugin: ‘java‘ sourceCompatibility = 1.6 version = ‘1.0‘ repositories { mavenCentral() } dependencies { compile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-core:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-logging-juli:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-el:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-websocket:8.0.9") compile("org.eclipse.jdt.core.compiler:ecj:4.4") }
compile("org.eclipse.jdt.core.compiler:ecj:4.4")相当重要,没有此jar包,类加载器就没有加载部分类的字节码,我多次调试才找到问题. 当然,也可以建一个java工程,到http://tomcat.apache.org/download-80.cgi下载Binary Distributions的Embedded包(下载地址:http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.9/bin/embed/apache-tomcat-8.0.9-embed.zip),把里面的所有jar包加到工程的Libraries就可以了. b.新建一个含有main方法的可执行类(此类的编写参考了Aleksa Vukotic James Goodwill写的<<Apache Tomcat 7>>CHAPTER9 Embedding Tomcat).
- import org.apache.catalina.LifecycleException;
- import org.apache.catalina.core.AprLifecycleListener;
- import org.apache.catalina.core.StandardServer;
- import org.apache.catalina.startup.ClassLoaderFactory;
- import org.apache.catalina.startup.Tomcat;
- import javax.servlet.ServletException;
- /**
- * Created by nil on 2014/8/1.
- */
- public class EmbeddedTomcat {
- private Tomcat tomcat;
- private void startTomcat(int port,String contextPath,String baseDir) throws ServletException, LifecycleException {
- tomcat = new Tomcat();
- tomcat.setPort(port);
- tomcat.setBaseDir(".");
- StandardServer server = (StandardServer) tomcat.getServer();
- AprLifecycleListener listener = new AprLifecycleListener();
- server.addLifecycleListener(listener);
- tomcat.addWebapp(contextPath, baseDir);
- tomcat.start();
- }
- private void stopTomcat() throws LifecycleException {
- tomcat.stop();
- }
- public static void main(String[] args) {
- try {
- int port=8080;
- String contextPath = "/test";
- String baseDir = "C:/Users/nil/AppData/Local/Temp/jetty-0.0.0.0-8080-test-1.0.war-_test-any-8966644989981380511.dir/webapp";
- EmbeddedTomcat tomcat = new EmbeddedTomcat();
- tomcat.startTomcat(port, contextPath, baseDir);
- //由于Tomcat的start方法为非阻塞方法,加一个线程睡眠模拟线程阻塞.
- Thread.sleep(10000000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
import org.apache.catalina.LifecycleException; import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.core.StandardServer; import org.apache.catalina.startup.ClassLoaderFactory; import org.apache.catalina.startup.Tomcat; import javax.servlet.ServletException; /** * Created by nil on 2014/8/1. */ public class EmbeddedTomcat { private Tomcat tomcat; private void startTomcat(int port,String contextPath,String baseDir) throws ServletException, LifecycleException { tomcat = new Tomcat(); tomcat.setPort(port); tomcat.setBaseDir("."); StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); tomcat.addWebapp(contextPath, baseDir); tomcat.start(); } private void stopTomcat() throws LifecycleException { tomcat.stop(); } public static void main(String[] args) { try { int port=8080; String contextPath = "/test"; String baseDir = "C:/Users/nil/AppData/Local/Temp/jetty-0.0.0.0-8080-test-1.0.war-_test-any-8966644989981380511.dir/webapp"; EmbeddedTomcat tomcat = new EmbeddedTomcat(); tomcat.startTomcat(port, contextPath, baseDir); //由于Tomcat的start方法为非阻塞方法,加一个线程睡眠模拟线程阻塞. Thread.sleep(10000000); } catch (Exception e) { e.printStackTrace(); } } }
其中baseDir为前面war解压的路径,另两个参数port和contextPath是容易理解的. c.运行一下main方法,就可以通过浏览器来访问这个web应用了.
二.war包配置的嵌入式jetty例子. 1.运行一下war任务,对这个web应用打成war包. 2.再新建一个gradle项目. a.build.gradle
- apply plugin: ‘java‘
- sourceCompatibility = 1.6
- version = ‘1.0‘
- repositories {
- mavenCentral()
- }
- dependencies {
- compile("org.eclipse.jetty:example-jetty-embedded:9.2.1.v20140609")
- compile("org.eclipse.jetty:jetty-jsp:9.2.1.v20140609")
- }
apply plugin: ‘java‘ sourceCompatibility = 1.6 version = ‘1.0‘ repositories { mavenCentral() } dependencies { compile("org.eclipse.jetty:example-jetty-embedded:9.2.1.v20140609") compile("org.eclipse.jetty:jetty-jsp:9.2.1.v20140609") }
b.新建一个含有main方法的可执行类(此类的编写主要参考jetty官方文档:http://www.eclipse.org/jetty/documentation/current/using-annotations-embedded.html).
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.webapp.*;
- /**
- * Created by nil on 2014/8/2.
- */
- public class Main {
- //Create a WebApp
- private static WebAppContext getWebAppContext(){
- WebAppContext webapp = new WebAppContext();
- webapp.setContextPath("/");
- webapp.setWar("E:/idea/test/build/libs/test-1.0.war");
- return webapp;
- }
- public static void appStart() throws Exception {
- Server server = new Server(8080);
- //Enable parsing of jndi-related parts of web.xml and jetty-env.xml
- org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
- classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
- classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
- //get a WebAppContext
- server.setHandler(getWebAppContext());
- server.start();
- server.join();
- }
- public static void main(String[] args) throws Exception {
- appStart();
- }
- }
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.*; /** * Created by nil on 2014/8/2. */ public class Main { //Create a WebApp private static WebAppContext getWebAppContext(){ WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setWar("E:/idea/test/build/libs/test-1.0.war"); return webapp; } public static void appStart() throws Exception { Server server = new Server(8080); //Enable parsing of jndi-related parts of web.xml and jetty-env.xml org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server); classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration"); classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration"); //get a WebAppContext server.setHandler(getWebAppContext()); server.start(); server.join(); } public static void main(String[] args) throws Exception { appStart(); } }
appStart方法内第3-5行是为了支持servlet3特性.其中上面的setWar这里就是上面打包的war包路径. c.运行一下main方法,就可以通过浏览器来访问这个web应用了.