我目前正在Tomcat6上运行我的webapps,我想评估一下在嵌入式模式下运行Tomcat。
除了api documentation中的内容之外,还有没有好的教程或其他资源
发布于 2009-03-13 00:31:20
代码不言而喻。请参阅pom.xml代码片段和运行tomcat的类。
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>catalina</artifactId>
<version>6.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>coyote</artifactId>
<version>6.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jasper</artifactId>
<version>6.0.18</version>
<scope>test</scope>
</dependency>
public class RunWebApplicationTomcat {
private String path = null;
private Embedded container = null;
private Log logger = LogFactory.getLog(getClass());
/**
* The directory to create the Tomcat server configuration under.
*/
private String catalinaHome = "tomcat";
/**
* The port to run the Tomcat server on.
*/
private int port = 8089;
/**
* The classes directory for the web application being run.
*/
private String classesDir = "target/classes";
/**
* The web resources directory for the web application being run.
*/
private String webappDir = "mywebapp";
/**
* Creates a single-webapp configuration to be run in Tomcat on port 8089. If module name does
* not conform to the 'contextname-webapp' convention, use the two-args constructor.
*
* @param contextName without leading slash, for example, "mywebapp"
* @throws IOException
*/
public RunWebApplicationTomcat(String contextName) {
Assert.isTrue(!contextName.startsWith("/"));
path = "/" + contextName;
}
/**
* Starts the embedded Tomcat server.
*
* @throws LifecycleException
* @throws MalformedURLException if the server could not be configured
* @throws LifecycleException if the server could not be started
* @throws MalformedURLException
*/
public void run(int port) throws LifecycleException, MalformedURLException {
this.port = port;
// create server
container = new Embedded();
container.setCatalinaHome(catalinaHome);
container.setRealm(new MemoryRealm());
// create webapp loader
WebappLoader loader = new WebappLoader(this.getClass().getClassLoader());
if (classesDir != null) {
loader.addRepository(new File(classesDir).toURI().toURL().toString());
}
// create context
// TODO: Context rootContext = container.createContext(path, webappDir);
Context rootContext = container.createContext(path, webappDir);
rootContext.setLoader(loader);
rootContext.setReloadable(true);
// create host
// String appBase = new File(catalinaHome, "webapps").getAbsolutePath();
Host localHost = container.createHost("localHost", new File("target").getAbsolutePath());
localHost.addChild(rootContext);
// create engine
Engine engine = container.createEngine();
engine.setName("localEngine");
engine.addChild(localHost);
engine.setDefaultHost(localHost.getName());
container.addEngine(engine);
// create http connector
Connector httpConnector = container.createConnector((InetAddress) null, port, false);
container.addConnector(httpConnector);
container.setAwait(true);
// start server
container.start();
// add shutdown hook to stop server
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
stopContainer();
}
});
}
/**
* Stops the embedded Tomcat server.
*/
public void stopContainer() {
try {
if (container != null) {
container.stop();
}
} catch (LifecycleException exception) {
logger.warn("Cannot Stop Tomcat" + exception.getMessage());
}
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public static void main(String[] args) throws Exception {
RunWebApplicationTomcat inst = new RunWebApplicationTomcat("mywebapp");
inst.run(8089);
}
public int getPort() {
return port;
}
}
发布于 2009-07-03 16:07:31
这可能会有帮助。
如果你下载Tomcat6.x的源包,你会得到这个类:
[http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/startup/Catalina.html#main(java.lang.String[])](http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/startup/Catalina.html#main(java.lang.String[]%29)
这是如何使用Embedd类的一个示例:it a shell to stop|start a specific Tomcat its。(我的意思是,您可以将CATALINA_BASE
设置为指向现有的Tomcat安装)。
如果你编译这段代码,你可以像这样运行:
java -D"catalina.base=%CATALINA_BASE%“-D"catalina.home=%CATALINA_HOME%”org.apache.catalina.startup.Catalina start
我还不确定如何修改这段代码来关闭服务器!
发布于 2012-03-16 23:44:24
几个月前读完这篇文章后,我写了这个项目:spring-embedded-tomcat。它可用于将tomcat6嵌入到基于Spring的应用程序中。
https://stackoverflow.com/questions/640022
复制相似问题