Cargo: finding out the state of an already-installed container
I'm trying to use Cargo to manage 开发者_如何学编程containers from a webapp on the same machine as the containers. So far, I've been able to do most things, but I'm stuck when it comes to finding out the state of container when I first start my manager app. When I first create a Container instance, container.getState() always returns State.UNKNOWN. The problem is that an extra JVM is created if I try to start a container that's already started, and my app hangs if I try to stop a container that is not currently running. My approach is like the following:
ConfigurationFactory configurationFactory = new DefaultConfigurationFactory();
LocalConfiguration configuration = (LocalConfiguration) configurationFactory.createConfiguration(containerId, ContainerType.INSTALLED, ConfigurationType.STANDALONE, CARGO_CONFIGURATION_DIRECTORY);
DefaultContainerFactory containerFactory = new DefaultContainerFactory();
InstalledLocalContainer container = (InstalledLocalContainer) containerFactory.createContainer(containerId, ContainerType.INSTALLED, configuration);
assert container.getState() == State.UNKNOWN;
Is there a way to find out what the actual state of the container is? The only information I can find about a container's state (before actually starting/stopping the container) is whether it's installed (via ZipURLInstaller.isAlreadyInstalled()).
The "solution" I ended up using was to ping the container using Cargo's internal APIs. Because the containers in my app were installed with Cargo, I ping the "Cargo Ping Component" app that Cargo deploys in each container it installs.
import org.codehaus.cargo.container.internal.util.HttpUtils;
import org.codehaus.cargo.container.spi.util.ContainerUtils;
...
URL cargoPingUrl = ContainerUtils.getCPCURL(getConfiguration());
return new HttpUtils().ping(cargoPingUrl);
It works so far.
精彩评论