Created
June 11, 2014 13:10
-
-
Save joakime/d6223a05b92f41c7cc80 to your computer and use it in GitHub Desktop.
Jetty Embedded Example of setting up Connectors for Stats gathering, X-Forwarded, and Low Resource handling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jetty; | |
import org.eclipse.jetty.server.Connector; | |
import org.eclipse.jetty.server.ConnectorStatistics; | |
import org.eclipse.jetty.server.ForwardedRequestCustomizer; | |
import org.eclipse.jetty.server.HttpConfiguration; | |
import org.eclipse.jetty.server.HttpConnectionFactory; | |
import org.eclipse.jetty.server.LowResourceMonitor; | |
import org.eclipse.jetty.server.SecureRequestCustomizer; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.ServerConnector; | |
import org.eclipse.jetty.server.SslConnectionFactory; | |
import org.eclipse.jetty.server.handler.StatisticsHandler; | |
import org.eclipse.jetty.util.component.Container; | |
import org.eclipse.jetty.util.ssl.SslContextFactory; | |
public class StatsConnector | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
// Since this example shows off SSL configuration, we need a keystore with the appropriate key. These two | |
// lines are purely a hack to get access to a keystore that we use in many unit tests and should probably be | |
// a direct path to your own keystore (used on line 29). | |
String jetty_home = System.getProperty("jetty.home","/path/to/jetty/home"); | |
System.setProperty("jetty.home", jetty_home); | |
String jetty_base = System.getProperty("jetty.home","/path/to/jetty/base"); | |
System.setProperty("jetty.home", jetty_base); | |
// Create a basic jetty server object without declaring the port. Since we are configuring connectors | |
// directly we'll be setting ports on those connectors. | |
Server server = new Server(); | |
// HTTP Configuration | |
// HttpConfiguration is a collection of configuration information appropriate for http and https. The default | |
// scheme for http is <code>http</code> of course, as the default for secured http is <code>https</code> but | |
// we show setting the scheme to show it can be done. The port for secured communication is also set here. | |
HttpConfiguration http_config = new HttpConfiguration(); | |
http_config.setSecureScheme("https"); | |
http_config.setSecurePort(8443); | |
http_config.setOutputBufferSize(32768); | |
http_config.setRequestHeaderSize(32768); // request header size | |
// Enable the X-Forwarded header customization | |
ForwardedRequestCustomizer forwarded = new ForwardedRequestCustomizer(); | |
http_config.addCustomizer(forwarded); | |
// HTTP connector | |
// The first server connector we create is the one for http, passing in the http configuration we configured | |
// above so it can get things like the output buffer size, etc. We also set the port (8080) and configure an | |
// idle timeout. | |
int acceptorCount = 8; // acceptor thread counts | |
int selectorCount = -1; // allow connector defaults | |
ServerConnector http = new ServerConnector(server,null,null,null,acceptorCount,selectorCount,new HttpConnectionFactory(http_config)); | |
http.setPort(8080); | |
http.setIdleTimeout(30000); // idle timeout | |
http.setAcceptQueueSize(8); // also known as backlog | |
// SSL Context Factory for HTTPS and SPDY | |
// SSL requires a certificate so we configure a factory for ssl contents with information pointing to what | |
// keystore the ssl connection needs to know about. Much more configuration is available the ssl context, | |
// including things like choosing the particular certificate out of a keystore to be used. | |
SslContextFactory sslContextFactory = new SslContextFactory(); | |
sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore"); | |
sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); | |
sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g"); | |
// HTTPS Configuration | |
// A new HttpConfiguration object is needed for the next connector and you can pass the old one as an | |
// argument to effectively clone the contents. On this HttpConfiguration object we add a | |
// SecureRequestCustomizer which is how a new connector is able to resolve the https connection before | |
// handing control over to the Jetty Server. | |
HttpConfiguration https_config = new HttpConfiguration(http_config); | |
https_config.addCustomizer(new SecureRequestCustomizer()); // configure HttpSerletRequest with ssl & certificate info | |
// HTTPS connector | |
// We create a second ServerConnector, passing in the http configuration we just made along with the | |
// previously created ssl context factory. Next we set the port and a longer idle timeout. | |
ServerConnector https = new ServerConnector(server, | |
new SslConnectionFactory(sslContextFactory,"http/1.1"), | |
new HttpConnectionFactory(https_config)); | |
https.setPort(8443); | |
https.setIdleTimeout(500000); | |
https.setAcceptQueueSize(8); | |
// Here you see the server having multiple connectors registered with it, now requests can flow into the server | |
// from both http and https urls to their respective ports and be processed accordingly by jetty. A simple | |
// handler is also registered with the server so the example has something to pass requests off to. | |
// Set the connectors | |
server.setConnectors(new Connector[] { http, https }); | |
// Enable Statistics Gathering for connectors | |
ConnectorStatistics.addToAllConnectors(server); | |
// Enable Low Resources Management | |
LowResourceMonitor lowResources = new LowResourceMonitor(server); | |
lowResources.setLowResourcesIdleTimeout(10000); | |
lowResources.setMaxConnections(10000); | |
server.addBean(lowResources); | |
// Set a handler | |
StatisticsHandler statsHandler = new StatisticsHandler(); | |
statsHandler.setHandler(new HelloHandler("Hi There")); | |
server.setHandler(statsHandler); | |
server.setHandler(new HelloHandler("Hi There")); | |
// Start the server | |
server.start(); | |
server.join(); | |
// Dump connector stats after server is done running | |
for (Connector connector : server.getConnectors()) | |
{ | |
if (connector instanceof Container) | |
{ | |
Container container = (Container)connector; | |
ConnectorStatistics stats = container.getBean(ConnectorStatistics.class); | |
System.out.printf("Connector: %s%n",connector); | |
stats.dump(System.out," "); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment