我们写了一个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

  1. apply plugin: 'java'
  2. sourceCompatibility = 1.6
  3. version = '1.0'
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. compile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.9")
  9. compile("org.apache.tomcat.embed:tomcat-embed-core:8.0.9")
  10. compile("org.apache.tomcat.embed:tomcat-embed-logging-juli:8.0.9")
  11. compile("org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.0.9")
  12. compile("org.apache.tomcat.embed:tomcat-embed-el:8.0.9")
  13. compile("org.apache.tomcat.embed:tomcat-embed-websocket:8.0.9")
  14. compile("org.eclipse.jdt.core.compiler:ecj:4.4")
  15. }
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).

  1. import org.apache.catalina.LifecycleException;
  2. import org.apache.catalina.core.AprLifecycleListener;
  3. import org.apache.catalina.core.StandardServer;
  4. import org.apache.catalina.startup.ClassLoaderFactory;
  5. import org.apache.catalina.startup.Tomcat;
  6. import javax.servlet.ServletException;
  7. /**
  8. * Created by nil on 2014/8/1.
  9. */
  10. public class EmbeddedTomcat {
  11. private Tomcat tomcat;
  12. private void startTomcat(int port,String contextPath,String baseDir) throws ServletException, LifecycleException {
  13. tomcat = new Tomcat();
  14. tomcat.setPort(port);
  15. tomcat.setBaseDir(".");
  16. StandardServer server = (StandardServer) tomcat.getServer();
  17. AprLifecycleListener listener = new AprLifecycleListener();
  18. server.addLifecycleListener(listener);
  19. tomcat.addWebapp(contextPath, baseDir);
  20. tomcat.start();
  21. }
  22. private void stopTomcat() throws LifecycleException {
  23. tomcat.stop();
  24. }
  25. public static void main(String[] args) {
  26. try {
  27. int port=8080;
  28. String contextPath = "/test";
  29. String baseDir = "C:/Users/nil/AppData/Local/Temp/jetty-0.0.0.0-8080-test-1.0.war-_test-any-8966644989981380511.dir/webapp";
  30. EmbeddedTomcat tomcat = new EmbeddedTomcat();
  31. tomcat.startTomcat(port, contextPath, baseDir);
  32. //由于Tomcat的start方法为非阻塞方法,加一个线程睡眠模拟线程阻塞.
  33. Thread.sleep(10000000);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
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

  1. apply plugin: 'java'
  2. sourceCompatibility = 1.6
  3. version = '1.0'
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. compile("org.eclipse.jetty:example-jetty-embedded:9.2.1.v20140609")
  9. compile("org.eclipse.jetty:jetty-jsp:9.2.1.v20140609")
  10. }
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).

  1. import org.eclipse.jetty.server.Server;
  2. import org.eclipse.jetty.webapp.*;
  3. /**
  4. * Created by nil on 2014/8/2.
  5. */
  6. public class Main {
  7. //Create a WebApp
  8. private static WebAppContext getWebAppContext(){
  9. WebAppContext webapp = new WebAppContext();
  10. webapp.setContextPath("/");
  11. webapp.setWar("E:/idea/test/build/libs/test-1.0.war");
  12. return webapp;
  13. }
  14. public static void appStart() throws Exception {
  15. Server server = new Server(8080);
  16. //Enable parsing of jndi-related parts of web.xml and jetty-env.xml
  17. org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
  18. classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
  19. classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
  20. //get a WebAppContext
  21. server.setHandler(getWebAppContext());
  22. server.start();
  23. server.join();
  24. }
  25. public static void main(String[] args) throws Exception {
  26. appStart();
  27. }
  28. }
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应用了.

05-11 18:02