如何用Java启动和停止Tomcat容器? [英] How to start and stop an Tomcat container with Java?

查看:256
本文介绍了如何用Java启动和停止Tomcat容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

所以,是可以使用java语句启动和停止容器吗?

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

推荐答案


那么,是否可能使用java语句启动和停止容器?

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

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

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.

要启动Tomcat容器并部署你的战争,你可以在 setUp()方法中执行类似的操作:

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

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

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

这篇关于如何用Java启动和停止Tomcat容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆