Embedding Jetty 9 and customizing Socket Address,

2019-08-05 17:58发布

问题:

I have previously used Jetty 8.1.14 as embedded web server in my application. Now I am trying to upgrade to version 9.2.10.

With Jetty 8, it was possible to specify the Host Address and Port using the setters in the "SelectChannelConnector" or "SslSelectChannelConnector", and also the ThreadPool as a constructor argument in the "Server" class.

Now, it seems one can only specify one or the other in the "Server" class. There are only constructor variants for the address and/or port, or the ThreadPool to use. I can't find any variant with all three arguments.

How can I specify all those parameters with Jetty 9? I have tried:

String bindAddress = "myValue";
int port = 12345;
Server s = new Server(new InetSocketAddress(bindAdress, port));

and

ThreadPool t = MyHighlyCustomizedThreadPool();
Server s = new Server(t);

回答1:

Use the ServerConnector for setting listen ports, setting listen host addresses, setting idle timeouts, and setting default protocols. Once started, the same connector can be used to determine actual listening port (if using dynamically assigned ports), actual listen hosts (if using dynamic host addresses), etc ...

Use the HttpConfiguration for setting buffers, secure identification (used for secure redirects), tweaking headers, powered by, server version announcement, etc..

Use ConnectionFactory implementations to control how the progression of protocol selection should work with a recently accepted incoming connection. (Yes, this is a thing that is important in today's Web infrastructure)

For a basic example of HTTP/1.1 and SSL+HTTP/1.1 (aka HTTPS) see the ManyConnectors.java embedded example

For an example of ConnectionFactory behavior with SPDY, see the SpdyConnector.java example (note: SPDY has been deprecated in favor of HTTP/2 in Jetty 9.3.x)

For an example of ConnectionFactory behavior with TLS + ALPN + HTTP/2, see the Http2Server.java example (note: you'll need Jetty 9.3.x for this)