Atitit 嵌入式tomcat 嵌入式服务器 attilax 你感觉艾提拉 总结
比起嵌入jetty ,文件可以自动刷新貌似还不错。。方便调试debug
package com.attilax.web;
import java.io.File;
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 com.attilax.io.pathx;
import com.attilax.util.Global;
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) {
System.out.println("");
try {
int port = 80;
String contextPath = "/";
String baseDir =pathx.webAppPath_jensyegeor();
//new File("webroot").getAbsolutePath(); // 项目中web目录名称,以前版本为WebRoot、webapp、webapps,现在为WebContent
EmbeddedTomcat tomcat = new EmbeddedTomcat();
tomcat.startTomcat(port, contextPath, baseDir);
// 由于Tomcat的start方法为非阻塞方法,加一个线程睡眠模拟线程阻塞.
String heartbeat_str = "--tomcat run. heartbeat_str";
Global.heartbeatRecycle(heartbeat_str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置简单的嵌入式tomcat - CSDN博客.html