#TECH

Run multiple Java Web Apps using Jetty on Different Ports – Simple & Quick

Introduction

Java based web applications are very commonly deployed using Tomcat and Jetty. I have worked extensively with both Tomcat and Jetty. The aim of this write-up is to provide a simple guide to anybody looking to run multiple web applications on the same machine. You’ll get to quickly know, how to deploy multiple apps with each on different port on Jetty. Each web app will run in its own Jetty server instance.

In comparison to having single instance for multiple web apps, this is a far better approach, because it is easy to configure and maintain individual web app lifecycles and resources.

Download and Install Jetty

Jetty can be downloaded from eclipse site. Here is the link to Download jetty. Download portable tar.gz file and extract it on your location. I worked with -7.1.6.v20100715 version.

$ tar -xvzf jetty-7.1.6.v20100715.tar.gz (for me location of jetty server is /home/devtools/softwares/Jetty-7.1.6.v20100715)

Create Webapps

  1. Create Webapp1:
    1. Create a folder Webapp1 :
      $ mkdir /home/workspace/JettyProject/Webapp1
    2. Copy existing war file to given location.
      $ cp /home/myproject/webapp1.war /home/workspace/JettyProject/Webapp1/
    3. Copy default jetty.xml from jetty server configurations for Webapp1.
      $ cp /home/devtools/softwares/Jetty-7.1.6.v20100715/etc/jetty.xml /home/workspace/JettyProject/Webapp1
  2. Create Webapp2:
    1. Create a folder Webapp2
      mkdir /home/workspace/JettyProject/Webapp2
    2. Copy existing war file to given location.
      $ cp /home/myproject/webapp2.war /home/workspace/JettyProject/Webapp2/
    3. Copy default jetty.xml from jetty server configurations for Webapp2.
      $ cp /home/devtools/softwares/Jetty-7.1.6.v20100715/etc/jetty.xml /home/workspace/JettyProject/Webapp2

Configure jetty

$ cd /home/devtools/softwares/Jetty-7.1.6.v20100715/contexts
$ mkdir webapp1
$ cd webapp1
$ create webapp1.xml and paste below content and save it.

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">/home/workspace/JettyProject/Webapp1</Set>
</Configure>

 

$ cd /home/devtools/softwares/Jetty-7.1.6.v20100715/contexts
$ mkdir webapp2
$ cd webapp2
$ create webapp2.xml and save given content in this file.

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/</Set>
  <Set name="war">/home/workspace/JettyProject/Webapp2</Set>
</Configure>

 

Configure port for each webapp

A) Configure webapp1 for 8080 port

$ cd /home/workspace/JettyProject/Webapp1
$ vi jetty.xml

  1. Find jetty.port and replace with your port. For my jetty.xml, here are the changes-
<Property name="jetty.port" default="8080"/>

 

  1. Add given bean in the jetty.xml and save it.

<Call name="addBean">
        	<Arg>
         	<New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
           	<Set name="contexts">
           	<Ref id="Contexts"/>
           </Set>
           	<Call name="setContextAttribute">
                     <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
                     <Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar$</Arg>
                </Call>
                <!-- Providers of Apps via Context XML files.
        Configured to behave similar to the legacy ContextDeployer -->
                <Call name="addAppProvider">
                    <Arg>
                        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                            <Set name="monitoredDir"><Property name="jetty.home" default="~/home/devtools/softwares/Jetty-7.1.6.v20100715"/>/contexts/webapp1
                            </Set>
                            <Set name="scanInterval">5</Set>
                        </New>
                    </Arg>
                </Call>
            </New>
        </Arg>
    </Call>

 

B) Configure webapp2 for 8099 port.

$ cd /home/workspace/JettyProject/Webapp2
$ vi jetty.xml

  1. Find jetty.port and replace with your port. For my jetty.xml, here are the changes:
<Property name="jetty.port" default="8099"/>

 

  1. Add given bean in the jetty.xml and save it.
<Call name="addBean">
        <Arg>
            <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
                <Set name="contexts">
                    <Ref id="Contexts"/>
                </Set>
                <Call name="setContextAttribute">
                    <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
                    <Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar$</Arg>
                </Call>
                <!-- Providers of Apps via Context XML files.
        Configured to behave similar to the legacy ContextDeployer -->
                <Call name="addAppProvider">
                    <Arg>
                        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                            <Set name="monitoredDir"><Property name="jetty.home" default="~/home/devtools/softwares/Jetty-7.1.6.v20100715"/>/contexts/webapp2
                            </Set>
                            <Set name="scanInterval">5</Set>
                        </New>
                    </Arg>
                </Call>
            </New>
        </Arg>
    </Call>

Now, both the webapps have been configured for 8080 and 8099 port. We can manage to start and stop both the apps separately. Both the application contexts are separate and independent.

Start Jetty Instance for each webapp

  1. Start Webapp1:
    $ /usr/bin/java -jar ~/home/devtools/softwares/Jetty-7.1.6.v20100715/start.jar /home/workspace/JettyProject/Webapp1/jetty.xml
  2. Start Webapp2:
    $ /usr/bin/java -jar ~/home/devtools/softwares/Jetty-7.1.6.v20100715/start.jar /home/workspace/JettyProject/Webapp2/jetty.xml

Code reference:

Working jetty.xml file.

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<!-- =============================================================== -->
<!-- Configure the Jetty Server                                      -->
<!--                                                                 -->
<!-- Documentation of this file format can be found at:              -->
<!-- http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax        -->
<!--                                                                 -->
<!-- Additional configuration files are available in $JETTY_HOME/etc -->
<!-- and can be mixed in.  For example:                              -->
<!--   java -jar start.jar etc/jetty.xml etc/jetty-ssl.xml           -->
<!--                                                                 -->
<!-- See start.ini file for the default configuraton files           -->
<!-- =============================================================== -->


<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Server Thread Pool                                          -->
    <!-- =========================================================== -->
    <Set name="ThreadPool">
        <!-- Default queued blocking threadpool -->
        <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
            <Set name="minThreads">10</Set>
            <Set name="maxThreads">200</Set>
        </New>
    </Set>

    <Call class="java.lang.System" name="setProperty">
        <Arg>org.eclipse.jetty.server.Request.maxFormContentSize</Arg>
        <Arg>500000</Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- Set connectors                                              -->
    <!-- =========================================================== -->

    <Call name="addConnector">
        <Arg>
            <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <Set name="host">
                    <Property name="jetty.host"/>
                </Set>
                <Set name="port">
                    <Property name="jetty.port" default="8080"/>
                </Set>
                <Set name="maxIdleTime">300000</Set>
                <Set name="Acceptors">2</Set>
                <Set name="statsOn">false</Set>
                <Set name="confidentialPort">8443</Set>
                <Set name="lowResourcesConnections">20000</Set>
                <Set name="lowResourcesMaxIdleTime">5000</Set>
            </New>
        </Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- Set handler Collection Structure                            -->
    <!-- =========================================================== -->
    <Set name="handler">
        <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <Set name="handlers">
                <Array type="org.eclipse.jetty.server.Handler">
                    <Item>
                        <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
                    </Item>
                    <Item>
                        <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
                    </Item>
                </Array>
            </Set>
        </New>
    </Set>

    <Call name="addBean">
        <Arg>
            <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
                <Set name="contexts">
                    <Ref id="Contexts"/>
                </Set>
                <Call name="setContextAttribute">
                    <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
                    <Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar$</Arg>
                </Call>
                <!-- Providers of Apps via Context XML files.
        Configured to behave similar to the legacy ContextDeployer -->
                <Call name="addAppProvider">
                    <Arg>
                        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                            <Set name="monitoredDir"><Property name="jetty.home" default="."/>/contexts/cp
                            </Set>
                            <Set name="scanInterval">5</Set>
                        </New>
                    </Arg>
                </Call>
            </New>
        </Arg>
    </Call>
    <!-- =========================================================== -->
    <!-- extra options                                               -->
    <!-- =========================================================== -->
    <Set name="stopAtShutdown">true</Set>
    <Set name="sendServerVersion">true</Set>
    <Set name="sendDateHeader">true</Set>
    <Set name="gracefulShutdown">1000</Set>

</Configure>

 

Enjoy experience with jetty!!!

You might also like