Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Jetty/Howto/WebappPerConnector

< Jetty‎ | Howto
Revision as of 15:00, 23 April 2013 by Jesse.mcconnell.gmail.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Serving Webapp A Only from Port A and Webapp B Only from Port B

Warning2.png
Some or all of this content remains to be ported to Jetty 9 Documentation.
If you are interested in migrating this content see our contribution guide or contact us.


The most efficient way to do this is with two org.eclipse.jetty.server.Server instances. There is also another, slightly less efficient alternative.

Server instance A has a connector listening on port A with webapp A defined, and Server instance B has webapp B and a connector listening on port B defined. For example:

We want to serve webapp A from port 8080, and webapp B from an SSL connector on port 8443. We set up two Server instances, each in its own jetty.xml file as follows:

jettyA.xml

<Configure id="ServerA" class="org.eclipse.jetty.server.Server">
 
    <!-- set up the port for ServerA -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">8080</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">10</Set>
          </New>
        </Item>
      </Array>
    </Set>
 
   <!-- set up a context provider for Server A -->
    <Call name="addLifeCycle">
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsA</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>
 
</Configure>

jettyB.xml:

<Configure id="ServerB" class="org.eclipse.jetty.server.Server">
 
    <!-- set up the port for ServerB -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New class="org.eclipse.jetty.server.ssl.SslSocketConnector">
            <Set name="Port">8443</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Keystore"><SystemProperty name="jetty.home" default="." />/etc/keystore</Set>
            <Set name="Password">OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4</Set>
            <Set name="KeyPassword">OBF:1u2u1wml1z7s1z7a1wnl1u2g</Set>
          </New>
        </Item>
      </Array>
    </Set>
 
     <!-- set up a context provider for ServerB -->
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsB</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>
 
</Configure>

Now we need to set up two context files, one to describe the webapp we want to deploy on Server A and the other for the webapp to deploy on Server B. We then put these files into $JETTY_HOME/contextsA or $JETTY_HOME/contextsB respectively.

contextA.xml

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/A</Set>
      <Set name="contextPath">/webappA</Set>
       ...
    </Configure>

contextB.xml:

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/B</Set>
      <Set name="contextPath">/webappB</Set>
       ...
    </Configure>

You run both Server instances in the same JVM by providing them both on the runline:

java -jar start.jar jettyA.xml jettyB.xml

Of course, you could also start two separate Jetty instances, one with jettyA.xml and the other with jettyB.xml. However, it is usually more efficient to run both Servers in the same JVM.

Alternative Method

There is also an alternative way to achieve the same result as above, however it is slightly less efficient. It involves setting the list of connectors on a webapp from which it will accept requests. This is a less efficient solution than the one described above because the request is presented to each webapp, which then must decide to accept it or not. In the first solution, only one webapp is ever passed the request. In this configuration, you only need a single Server instance. You define all of the connectors with a unique name, and then assign each webapp a list of connector names to which it should respond.

Here's how to configure the alternative solution:

jetty.xml:

<Configure class="org.eclipse.jetty.server.Server">
 
    <!-- set up both connectors -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New  class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">8080</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">1</Set>
            <Set name="name">connA</Set>
          </New>
        </Item>
        <Item>
          <New id="connB" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">9090</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">1</Set>            
            <Set name="name">connB</Set>
          </New>
        </Item>
      </Array>
    </Set>
 
     <!-- set up a context provider -->
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>
</Configure>


contextA.xml:

    <Configure  class="org.eclipse.jetty.webapp.WebAppContext">      
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/A</Set>
      <Set name="contextPath">/webappA</Set>
      <Set name="connectorNames">
        <Array type="String">
          <Item>connA</Item>
        </Array>
       </Set>
      ...
    </Configure>

contextB.xml:

    <Configure  class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/B</Set>
      <Set name="contextPath"/webappB</Set>
      <Set name="connectorNames">
        <Array type="String">
          <Item>connB</Item>
        </Array>
      </Set>
    </Configure>

Now start jetty as usual (if your Server config file is called jetty.xml you can omit it from the run line):

 java -jar start.jar

Copyright © Eclipse Foundation, Inc. All Rights Reserved.