View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.embedded;
20  
21  import org.eclipse.jetty.io.ArrayByteBufferPool;
22  import org.eclipse.jetty.io.ByteBufferPool;
23  import org.eclipse.jetty.server.Connector;
24  import org.eclipse.jetty.server.ForwardedRequestCustomizer;
25  import org.eclipse.jetty.server.HttpConfiguration;
26  import org.eclipse.jetty.server.HttpConnectionFactory;
27  import org.eclipse.jetty.server.SecureRequestCustomizer;
28  import org.eclipse.jetty.server.Server;
29  import org.eclipse.jetty.server.ServerConnector;
30  import org.eclipse.jetty.server.SslConnectionFactory;
31  import org.eclipse.jetty.spdy.server.NPNServerConnectionFactory;
32  import org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnectionFactory;
33  import org.eclipse.jetty.spdy.server.http.PushStrategy;
34  import org.eclipse.jetty.spdy.server.http.ReferrerPushStrategy;
35  import org.eclipse.jetty.util.ssl.SslContextFactory;
36  import org.eclipse.jetty.util.thread.QueuedThreadPool;
37  import org.eclipse.jetty.util.thread.TimerScheduler;
38  
39  /* ------------------------------------------------------------ */
40  /**
41   * A Jetty server with multiple connectors.
42   *
43   */
44  public class ManyConnectors
45  {
46      public static void main(String[] args) throws Exception
47      {
48          String jetty_home = System.getProperty("jetty.home","../jetty-server/src/main/config");
49          System.setProperty("jetty.home", jetty_home);
50  
51          Server server = new Server();
52  
53          // HTTP connector
54          ServerConnector connector0 = new ServerConnector(server);
55          connector0.setPort(8080);
56          connector0.setIdleTimeout(30000);
57  
58          // HTTPS connector
59          SslContextFactory sslContextFactory = new SslContextFactory();
60          sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
61          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
62          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
63  
64          ServerConnector connector1 = new ServerConnector(server,sslContextFactory);
65          connector1.setPort(8443);
66  
67  
68          // A verbosely fully configured connector with SSL, SPDY and HTTP
69  
70          HttpConfiguration config = new HttpConfiguration();
71          config.setSecureScheme("https");
72          config.setSecurePort(8443);
73          config.setOutputBufferSize(32768);
74          config.setRequestHeaderSize(8192);
75          config.setResponseHeaderSize(8192);
76          config.addCustomizer(new ForwardedRequestCustomizer());
77          config.addCustomizer(new SecureRequestCustomizer());
78  
79          HttpConnectionFactory http = new HttpConnectionFactory(config);
80          http.setInputBufferSize(16384);
81  
82          PushStrategy push = new ReferrerPushStrategy();
83          HTTPSPDYServerConnectionFactory spdy2 = new HTTPSPDYServerConnectionFactory(2,config,push);
84          spdy2.setInputBufferSize(8192);
85          spdy2.setInitialWindowSize(32768);
86  
87          HTTPSPDYServerConnectionFactory spdy3 = new HTTPSPDYServerConnectionFactory(3,config,push);
88          spdy2.setInputBufferSize(8192);
89  
90          NPNServerConnectionFactory npn = new NPNServerConnectionFactory(spdy3.getProtocol(),spdy2.getProtocol(),http.getProtocol());
91          npn.setDefaultProtocol(http.getProtocol());
92          npn.setInputBufferSize(1024);
93  
94          SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory,npn.getProtocol());
95  
96          QueuedThreadPool threadPool = new QueuedThreadPool();
97          threadPool.setMaxThreads(256);
98          TimerScheduler scheduler = new TimerScheduler();
99          ByteBufferPool bufferPool= new ArrayByteBufferPool(32,4096,32768);
100 
101         ServerConnector connector2 = new ServerConnector(server,threadPool,scheduler,bufferPool,2,2,ssl,npn,spdy3,spdy2,http);
102         connector2.setDefaultProtocol("ssl-npn");
103         connector2.setPort(8444);
104         connector2.setIdleTimeout(30000);
105         connector2.setSoLingerTime(10000);
106 
107         // Set the connectors
108         server.setConnectors(new Connector[] { connector0, connector1, connector2 });
109 
110 
111         server.setHandler(new HelloHandler());
112 
113         server.start();
114         server.dumpStdErr();
115         server.join();
116     }
117 }