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

Difference between revisions of "Jetty/Howto/WebappPerConnector"

< Jetty‎ | Howto
(New page: ==How do I serve webapp A only from port A and webapp B only from port B?== The most efficient way to do this is with two org.eclipse.jetty.server.Server instances. There is also another,...)
 
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==How do I serve webapp A only from port A and webapp B only from port B?==
+
==Serving Webapp A Only from Port A and Webapp B Only from Port B==
  
The most efficient way to do this is with two org.eclipse.jetty.server.Server instances. There is also another, slightly less efficient alternative.
+
{{Jetty TODO}}
  
Server instance A will have a connector listening on port A with webapp A defined, and Server instance B will have webapp B and connector listening on port B defined.
+
The most efficient way to do this is with two <tt>org.eclipse.jetty.server.Server</tt> instances. There is also another, slightly less efficient [[#Alternative Method|alternative]].
  
Let's clarify that with an example.
+
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:
  
Suppose we have webapp A that we want served from port 8080, and webapp B that we want served from an SSL connector on port 8443. We would set up 2 different Server instances, each in it's own jetty xml file like so.
+
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 <tt>jetty.xml</tt> file as follows:
  
 
<b>jettyA.xml</b>
 
<b>jettyA.xml</b>
Line 27: Line 27:
 
     </Set>
 
     </Set>
  
   <!-- set up a context deployer for Server A -->
+
   <!-- set up a context provider for Server A -->
 
     <Call name="addLifeCycle">
 
     <Call name="addLifeCycle">
 
       <Arg>
 
       <Arg>
         <New class="org.eclipse.jetty.deploy.ContextDeployer">
+
         <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
 
           <Set name="contexts"><Ref id="Contexts"/></Set>
 
           <Set name="contexts"><Ref id="Contexts"/></Set>
 
           <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsA</Set>
 
           <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsA</Set>
Line 61: Line 61:
 
     </Set>
 
     </Set>
  
     <!-- set up a context deployer for ServerB -->
+
     <!-- set up a context provider for ServerB -->
 
       <Arg>
 
       <Arg>
         <New class="org.eclipse.jetty.deploy.ContextDeployer">
+
         <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
 
           <Set name="contexts"><Ref id="Contexts"/></Set>
 
           <Set name="contexts"><Ref id="Contexts"/></Set>
 
           <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsB</Set>
 
           <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsB</Set>
Line 74: Line 74:
 
</source>
 
</source>
  
Now we need to set up 2 context files, one to describe the webapp we wish 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.
+
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.
  
 
<b>contextA.xml</b>
 
<b>contextA.xml</b>
Line 99: Line 99:
 
{{codeblock|java -jar start.jar jettyA.xml jettyB.xml}}
 
{{codeblock|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, if you wanted to. However, it is usually more efficient to run both Servers in the same JVM.
+
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==
 
==Alternative Method==
  
There is also a 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 if it will accept it or not. In the first solution, only one webapp will ever be 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.
+
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:
 
Here's how to configure the alternative solution:
Line 134: Line 134:
 
     </Set>
 
     </Set>
  
     <!-- set up a context deployer -->
+
     <!-- set up a context provider -->
 
       <Arg>
 
       <Arg>
         <New class="org.eclipse.jetty.deploy.ContextDeployer">
+
         <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
 
           <Set name="contexts"><Ref id="Contexts"/></Set>
 
           <Set name="contexts"><Ref id="Contexts"/></Set>
 
           <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
 
           <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>

Latest revision as of 15:00, 23 April 2013

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.