Run multiple instances of Jetty on different ports on Ubuntu 9.04.

My goal today was to run multiple instances of Jetty on different ports on Ubuntu 9.04.

The Ubuntu 9.04 repository has Jetty 5.x only. It might be best to just uninstall the Jetty package and download a Jetty 6.x from the Jetty website. But if you are stuck with Jetty 5.x, this is how to get multiple instances running.

First, we want to learn how to launch Jetty from the command line. When you start Jetty with /etc/init.d/jetty start, what really happens is something like this:


/usr/lib/jvm/java-6-sun/bin/java -Xmx256m -Djava.awt.headless=true -Djava.io.tmpdir="/var/cache/jetty" -Djava.library.path=/usr/lib -DSTART=/etc/jetty/start.config -Djetty.home=/usr/share/jetty -jar /usr/share/jetty/lib/start.jar /etc/jetty/jetty.xml

depending on the Java envrionment on your system. You must know exactly what this command is so you can run it manually. Open /etc/init.d/jetty and have it print what it's doing by adding


echo "$JAVA $ARGUMENTS"

above the line:


su -p -s /bin/sh "$JETTY_USER" \
-c "$JAVA $ARGUMENTS >> $LOGDIR/out.log 2>&1 & \
echo \$!" > "$PIDFILE"
echo "$NAME."

Then do /etc/init.d/jetty restart and see what it prints out.

This long command of course boils down to:

jetty -jar start.jar /etc/jetty/jetty.xml

That last argument, the jetty.xml conf file, controls the id of the Jetty instance. We can launch multiple Jetty instances by starting Jetty with different ids. To make it easier on you to run this command and vary the conf file, create a script start.sh like this:


#!/bin/sh
/usr/lib/jvm/java-6-sun/bin/java -Xmx256m -Djava.awt.headless=true -Djava.io.tmpdir="/var/cache/jetty" -Djava.library.path=/usr/lib -DSTART=/etc/jetty/start.config -Djetty.home=/usr/share/jetty -jar /usr/share/jetty/lib/start.jar $1

This way we can run


$ start.sh /etc/jetty/jetty.xml

to launch Jetty. I would put start.sh in the /usr/share/jetty/lib directory.

If you look in the conf file you will see something like this:



It turns out that the Configure tag has an attribute named id. If two conf files have the same id, the conf is merged; if they have different ids, separate jetty instances are launched. So, copy the jetty.xml file to a jetty-a.xml file, and change the Configure line to:



You also want the Jetty instances to run on different ports, so change



to



However, if you try to use this conf file with


jetty -jar start.jar /etc/jetty/jetty-a.xml

you will get errors like this:


org.xml.sax.SAXParseException: Attribute "id" must be declared for element type "Configure".

The culprit is that Ubuntu/Debian jetty.xml hardcodes the Jetty configuration DTD version. According to the Jetty team, it is better to leave out the version; the code redirects it to the most recent version. So change


<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure 1.2//EN" "http://jetty.mortbay.org/configure_1_2.dtd">

to


<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

Now if you run


# start.sh /etc/jetty/jetty.xml &
# start.sh /etc/jetty/jetty-a.xml &

you will have two Jetty instances on separate ports.

References: