8

I just want to configure jetty to listen to more than one port. I don't want multiple instances nor multiple webapps, just one jetty, one webapp, but listening to 2 or more ports.

The default way does not support multiple entries:

<Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>

Thank you for your help!

Mario Bertschler
  • 205
  • 1
  • 3
  • 9

2 Answers2

11

In your jetty.xml file, add a new connector:

<!-- original connector on port 8080 -->
<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>

<!-- new connector on port 8081 --> 
<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="8081"/></Set>
        <Set name="maxIdleTime">300000</Set>
        <Set name="Acceptors">2</Set>
        <Set name="statsOn">false</Set>
    <Set name="lowResourcesConnections">20000</Set>
    <Set name="lowResourcesMaxIdleTime">5000</Set>
      </New>
  </Arg>
</Call>

Then start jetty

java -jar start.jar etc\jetty.xml

Should do what you want.

Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
  • For the googliness, I did this for the solr bundle. I think solr must be set up to by default read jetty.xml, so you don't even have to pass that in. Thanks! – Jason Dunkelberger Feb 13 '12 at 18:18
  • i have done something like that to add a port, but is it possible to insert a tag in there to monitor a different path for the second port? – mautrok Aug 21 '14 at 10:51
  • How to do it in Jetty 10 - To have one webapp and one jetty-base dir but listen on multiple ports? – Akshay Shah Sep 26 '22 at 15:52
5

And if using Jetty in embedded mode, you can open multiple ports in your Java code:

Server server = new Server();
Connector c1 = new SelectChannelConnector();
c1.setPort(8080);
Connector c2 = new SelectChannelConnector();
c2.setPort(8081);
/* ... even more ports ... */
Connector[] ports = {c1, c2 /* ... */};
server.setConnectors(ports);
Andi
  • 3,234
  • 4
  • 32
  • 37