如何使用 Java 启动和停止 Tomcat 容器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2190300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start and stop an Tomcat container with Java?
提问by Sebastian Rapp
I have a Maven project that starts a tomcat container for pre-integration-tests (jUnit Tests). Most of my tests require that the web-application under tests is restarted. So I'd like to restart the Tomcat container before each jUnit test is executed.
我有一个 Maven 项目,它启动一个用于预集成测试(jUnit 测试)的 tomcat 容器。我的大多数测试都需要重新启动被测 Web 应用程序。所以我想在执行每个 jUnit 测试之前重新启动 Tomcat 容器。
As for now I use the cargo-maven2-plugin to configure the tomcat container.
至于现在我使用cargo-maven2-plugin来配置tomcat容器。
So, is it possible to start and stop the container with a java statement?
那么,是否可以使用java语句启动和停止容器?
回答by Pascal Thivent
So, is it possible to start and stop the container with a java statement?
那么,是否可以使用java语句启动和停止容器?
Your use case looks extremely weird (having to restart the container between tests) but let's not discuss this. To answer your question, yes it is possible and this can be done using Cargo's Java API.
您的用例看起来非常奇怪(必须在测试之间重新启动容器),但我们不讨论这个。要回答您的问题,是的,这是可能的,并且可以使用Cargo的 Java API来完成。
To start a Tomcat container and deploy your war, you can do something like this in the setUp()method:
要启动 Tomcat 容器并部署您的War,您可以在setUp()方法中执行以下操作:
// (1) Optional step to install the container from a URL pointing to its distribution
Installer installer = new ZipURLInstaller(new URL("http://www.apache.org/dist/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip"));
installer.install();
// (2) Create the Cargo Container instance wrapping our physical container
LocalConfiguration configuration = (LocalConfiguration) new DefaultConfigurationFactory()
.createConfiguration("tomcat6x"), ContainerType.INSTALLED, ConfigurationType.STANDALONE);
container = (InstalledLocalContainer) new DefaultContainerFactory()
.createContainer("tomcat6x", ContainerType.INSTALLED, configuration);
container.setHome(installer.getHome());
// (3) Statically deploy some WAR (optional)
WAR deployable = new WAR("./webapp-testing-webapp/target/webapp-testing-webapp-1.0.war");
deployable.setContext("ROOT");
configuration.addDeployable(deployable);
// (4) Start the container
container.start();
And stop it in the tearDown()method.
并在tearDown()方法中停止它。
// (6) Stop the container
container.stop();
回答by Adisesha
bootstrap.jar and commons-logging-api-1.1.1 from tomcat\bin to your classpath and the following snippet may help,
bootstrap.jar 和 commons-logging-api-1.1.1 从 tomcat\bin 到您的类路径,以下代码段可能会有所帮助,
Bootstrap bootstrap=new Bootstrap();
bootstrap.setCatalinaHome("D:/apache-tomcat-5.5.28");
bootstrap.start();
Thread.sleep(60000);
bootstrap.stop();
回答by TimReset
I'm using follow method:
我正在使用以下方法:
private static final String TOMCAT = "D:/test_tomcat";
@BeforeClass
public static void runTomcat() {
bootstrap = new Bootstrap();
bootstrap.setCatalinaHome(TOMCAT);
try {
bootstrap.start();
} catch (Exception e) {
e.printStackTrace();
fail("Не удалось запустить Tomcat");
}
}
@AfterClass
public static void stopTomcat() {
try {
bootstrap.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
But is method have a one problem - I need copy WAR file in 'webapps' directory every time have change source code. I thing what copy my class from classpath in web-inf and copy 'jar' from classpath in web-inf/lib.
但是方法有一个问题 - 每次更改源代码时,我都需要在“webapps”目录中复制 WAR 文件。我想从 web-inf 中的类路径中复制我的类并从 web-inf/lib 中的类路径中复制“jar”。
回答by Adeel Ansari
Yes it is. But I am afriad, how much time will your tests take. You must fix your tests to not depend on server startup and shutdown. Its fine to run tests inside container, but container should be started once before tests.
是的。但我很害怕,你的测试需要多少时间。您必须修复您的测试,使其不依赖于服务器启动和关闭。在容器内运行测试是可以的,但是容器应该在测试之前启动一次。
Well to answer your question, you can execute the relevant .shor .batfiles from Java using system calls. Something like below,
好吧,回答您的问题,您可以使用系统调用从 Java执行相关.sh或.bat文件。像下面这样的,
Runtime r = Runtime.getRuntime();
Process p = r.exec("start.sh");
p.waitFor();

