Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure jetty to listen to multiple ports

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!

like image 922
Mario Bertschler Avatar asked Aug 01 '11 22:08

Mario Bertschler


People also ask

What port does jetty run on?

jetty , by default, it runs on port 8080, in root context '/'. 2.2 Change a different context path, set seconds to check for changes and automatically hot redeploy. 2.3 Change a different port to start.

How do I change the port for jetty in eclipse?

Changing a Jetty Server PortEither double click the port, or use the content menu. In the Port Number field, replace the current port number, 8080, with the new port number, in this example, 8081. In the main Eclipse toolbar, click File -> Save. You have changed the port.

Where is Jetty config file?

jetty. xml is the default configuration file for Jetty, typically located at $JETTY_HOME/etc/jetty. xml.


2 Answers

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.

like image 133
Nicolas Modrzyk Avatar answered Sep 25 '22 01:09

Nicolas Modrzyk


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);
like image 45
Andi Avatar answered Sep 24 '22 01:09

Andi