如何使用Java启动和停止Tomcat容器?

发布于 2024-08-20 05:58:00 字数 196 浏览 3 评论 0原文

我有一个 Maven 项目,它启动一个 tomcat 容器进行预集成测试(jUnit 测试)。我的大多数测试都要求重新启动正在测试的 Web 应用程序。所以我想在执行每个 jUnit 测试之前重新启动 Tomcat 容器。

目前我使用cargo-maven2-plugin来配置tomcat容器。

那么,是否可以用java语句来启动和停止容器呢?

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.

As for now I use the cargo-maven2-plugin to configure the tomcat container.

So, is it possible to start and stop the container with a java statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别低头,皇冠会掉 2024-08-27 05:58:00

那么,是否可以用java语句来启动和停止容器呢?

您的用例看起来非常奇怪(必须在测试之间重新启动容器),但我们不讨论这个。要回答您的问题,是的,这是可能的,这可以使用 Cargo 的 Java API 来完成。

要启动 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();

并在 tearDown() 方法中停止它。

// (6) Stop the container
container.stop();

So, is it possible to start and stop the container with a java statement?

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.

To start a Tomcat container and deploy your war, you can do something like this in the setUp() method:

// (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.

// (6) Stop the container
container.stop();
回忆追雨的时光 2024-08-27 05:58:00

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();

bootstrap.jar and commons-logging-api-1.1.1 from tomcat\bin to your classpath and the following snippet may help,

Bootstrap bootstrap=new Bootstrap();
bootstrap.setCatalinaHome("D:/apache-tomcat-5.5.28");

bootstrap.start();

Thread.sleep(60000);

bootstrap.stop();
雨落□心尘 2024-08-27 05:58:00

我正在使用以下方法:

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();
    }
}

但是该方法有一个问题 - 每次更改源代码时我都需要在“webapps”目录中复制 WAR 文件。
我想从 web-inf 中的类路径复制我的类,并从 web-inf/lib 中的类路径复制“jar”。

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.

审判长 2024-08-27 05:58:00

是的。但我担心,你的考试要花多少时间。您必须修复您的测试,使其不依赖于服务器启动和关闭。可以在容器内运行测试,但容器应该在测试之前启动一次。

好吧,回答你的问题,你可以使用系统调用从 Java 执行相关的 .sh.bat 文件。像下面这样的东西,

Runtime r = Runtime.getRuntime();
Process p = r.exec("start.sh");
p.waitFor();

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 .sh or .bat files from Java using system calls. Something like below,

Runtime r = Runtime.getRuntime();
Process p = r.exec("start.sh");
p.waitFor();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文