Home > English, java, work > The Cargo Daemon – For ‘intense’ redeployments with Maven

The Cargo Daemon – For ‘intense’ redeployments with Maven

When you’re doing continuous integration with frequent redeployments you might run into out of memory errors or some things that won’t unload properly. The Cargo daemon will solve this for you. Before reading any further you have to promise that this is no excuse to skip testing for memory leaks!
In this article I will explain how to deploy multiple applications in a Tomcat container including some jdbc-drivers. I will use the cargo-maven2-plugin since this is the easiest option when using Jenkins (or any other ci-environment).

I won’t go too deep into all the configuration options since the manual is pretty good. I just want to get you up and running a bit faster.

Starting/stopping the Cargo daemon

Installing the Cargo daemon is nothing more than downloading and running the cargo-daemon-webapp.war with java -jar.

Visit http://localhost:18000 to make sure the daemon is running. It will show a nice ship. This interface isn’t really important. You can see the status of the webapp and start/stop/delete your web container.

Since the container is started in the background I’m not really sure how to stop it properly. Right now I do an ‘lsof -i :18000’ and kill the process.

Deploying multiple wars

When deploying a single war you can include the cargo-maven2-plugin in your web project. Since we’re deploying multiple wars in our container I created a separate pom for deployment. This might also be a good idea when you want to separate building a war and doing the deployment.

This is the pom I ended up with :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.jvw.web</groupId>
    <artifactId>myserver-deployment</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Cargo Deployment for My Server @ Local Domain</name>

    <description>deployment via 'mvn cargo:daemon-start'</description>

    <properties>
        <server.hostname>myserver.localdomain</server.hostname>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.jvw.web</groupId>
            <artifactId>awesome-webapp</artifactId>
            <version>1.0.7</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>net.jvw.web</groupId>
            <artifactId>pretty-awesome-webapp</artifactId>
            <version>2.0.4</version>
            <type>war</type>
        </dependency>

        <!-- Container dependencies (jdbc drivers etc.)-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <configuration>
                    <configuration>
                        <properties>
                            <cargo.hostname>${server.hostname}</cargo.hostname>
                        </properties>
                    </configuration>
                    <container>
                        <containerId>tomcat7x</containerId>
                        <artifactInstaller>
                                <groupId>org.apache.tomcat</groupId>
                                <artifactId>tomcat</artifactId>
                                <version>7.0.50</version>
                        </artifactInstaller>
                        <!-- dependencies that will end up in the common/lib folder (ie jdbc-drivers) -->
                        <dependencies>
                            <dependency>
                                <groupId>mysql</groupId>
                                <artifactId>mysql-connector-java</artifactId>
                            </dependency>
                        </dependencies>
                    </container>
                    <daemon>
                        <properties>
                            <cargo.daemon.url>http://${server.hostname}:18000</cargo.daemon.url>
                            <cargo.daemon.handleid>myserver-deployments</cargo.daemon.handleid>
                        </properties>
                    </daemon>

                    <deployables>
                        <deployable>
                            <groupId>net.jvw.web</groupId>
                            <artifactId>awesome-webapp</artifactId>
                            <type>war</type>
                        </deployable>
                        <deployable>
                            <groupId>net.jvw.web</groupId>
                            <artifactId>pretty-awesome-webapp</artifactId>
                            <type>war</type>
                        </deployable>
                    </deployables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Let’s dissect this pom. The starting point is the . The configuration-tag is a bit confusing since it has an inner-tag with the same name and it looks like properties are a bit scattered around. The first section of properties (configuration/configuration) are the properties of your container (yes I’d expected them at the container-tag). The daemon-tag contains properties for the cargo-daemon. It might be a bit confusing, but just open a few tabs in your browser and look for the property you want to change. The handle id is the same id that will appear in the Cargo web interface.

Next section is the container-tag. Here you can define which container you want to use. The nice thing is that you can use a maven repository to download Tomcat and a mysql-driver (it’s also possible to point to a file system or url). I also included a mysql-jdbc-driver to show how to add something else than a war. Make sure that the dependencies are also included in the -section of your pom.

Final seciton is the deployables-tag. For this section you also have to include a deployable as a dependency.

it might look like a lot of xml but it actually is pretty easy to understand I think. This was all, run mvn cargo:daemon-start and your web applications will be deployed in an empty container that is downloaded from a maven repository. You can stop the container with the web interface. When you want to redeploy execute mvn cargo:daemon-start again, it will undeploy (and even kill stuff when the stuff isn’t listening) and restart the container.

When things go wrong

Of course things will go wrong some time. When you get errors with port 8205 there probably is another running Tomcat instance. This could be an old instance that you should kill or you made an error while adding another container. When you want to run more that one Tomcat instance keep in mind that Tomcat doesn’t only use port 8080. Also 8009 (ajp). To add another instance you can assign both these ports. Another option is to use /configuration/configuration/cargo.port.offset . When you set this to 1000 Tomcat will run on port 9080 (and ajp on 9009). This way you only have to change one number.

Cargo places all its files under /{user.home}/.cargo . This location might not be writable for the user that started the Cargo daemon. It also means that the user that started the daemon also starts the Tomcat container.

When you’re looking for the logfiles you have to look in the /{user.home}/.cargo/logs/{handle-id}/ directory.

Conclusion

All our problems deploying applications are solved and Cargo appeared to be a very easy solution to deploy applications. Just install and start the daemon and you’re ready to go! Combine it with CI (ie Jenkins) and you have a very powerful tool!

Sources

http://cargo.codehaus.org/Cargo+Daemon
http://cargo.codehaus.org/Maven2+Plugin+Reference+Guide

Categories: English, java, work Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: