Java Code Examples for org.eclipse.jetty.server.ServerConnector#setReuseAddress()

The following examples show how to use org.eclipse.jetty.server.ServerConnector#setReuseAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpServer2.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private ServerConnector createHttpChannelConnector(
    Server server, HttpConfiguration httpConfig) {
  ServerConnector conn = new ServerConnector(server,
      conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
      conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
  ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
  conn.addConnectionFactory(connFactory);
  if (Shell.WINDOWS) {
    // result of setting the SO_REUSEADDR flag is different on Windows
    // http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
    // without this 2 NN's can start on the same machine and listen on
    // the same port with indeterminate routing of incoming requests to them
    conn.setReuseAddress(false);
  }
  return conn;
}
 
Example 2
Source File: ApiWebSite.java    From dapeng-soa with Apache License 2.0 6 votes vote down vote up
public static Server createServer(int port) throws MalformedURLException, URISyntaxException {
    Server server = new Server();
    server.setStopAtShutdown(true);

    ServerConnector connector = new ServerConnector(server);
    connector.setPort(port);
    connector.setReuseAddress(true);

    server.setConnectors(new Connector[]{connector});

    WebAppContext webContext = new WebAppContext("webapp", CONTEXT);
    webContext.setBaseResource(Resource.newResource(new URL(ApiWebSite.class.getResource("/webapp/WEB-INF"), ".")));
    webContext.setClassLoader(ApiWebSite.class.getClassLoader());

    server.setHandler(webContext);

    return server;
}
 
Example 3
Source File: HttpServer2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private ServerConnector createHttpChannelConnector(
    Server server, HttpConfiguration httpConfig) {
  ServerConnector conn = new ServerConnector(server,
      conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
      conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
  ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
  conn.addConnectionFactory(connFactory);
  if(Shell.WINDOWS) {
    // result of setting the SO_REUSEADDR flag is different on Windows
    // http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
    // without this 2 NN's can start on the same machine and listen on
    // the same port with indeterminate routing of incoming requests to them
    conn.setReuseAddress(false);
  }
  return conn;
}
 
Example 4
Source File: ApiServer.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void installListeners(Server server, Collection<? extends InetSocketAddress> addresses) {
    final List<Connector> connectors = new ArrayList<>(addresses.size());

    for (InetSocketAddress address : addresses) {
        final ServerConnector server_connector = new ServerConnector(server);
        server_connector.setReuseAddress(true);
        if (address.getAddress() != null) {
            if (!address.getAddress().isAnyLocalAddress()) {
                LOG.log(Level.INFO, "Binding API server address: {0}", address.getAddress().getHostAddress());
                server_connector.setHost(address.getAddress().getHostAddress());
            }
        } else if (address.getHostString() != null) {
            LOG.log(Level.INFO, "Binding API server address name: {0}", address.getHostString());
            server_connector.setHost(address.getHostString());
        }
        LOG.log(Level.INFO, "Binding API server port: {0}", address.getPort());
        server_connector.setPort(address.getPort());
        connectors.add(server_connector);
    }

    server.setConnectors(connectors.toArray(new Connector[connectors.size()]));
}
 
Example 5
Source File: HttpServer2.java    From knox with Apache License 2.0 6 votes vote down vote up
private ServerConnector createHttpChannelConnector(
    Server server, HttpConfiguration httpConfig) {
  ServerConnector conn = new ServerConnector(server,
      conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
      conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
  ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
  conn.addConnectionFactory(connFactory);
  if(Shell.WINDOWS) {
    // result of setting the SO_REUSEADDR flag is different on Windows
    // http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
    // without this 2 NN's can start on the same machine and listen on
    // the same port with indeterminate routing of incoming requests to them
    conn.setReuseAddress(false);
  }
  return conn;
}
 
Example 6
Source File: HttpServer2.java    From knox with Apache License 2.0 6 votes vote down vote up
private ServerConnector createHttpChannelConnector(
    Server server, HttpConfiguration httpConfig) {
  ServerConnector conn = new ServerConnector(server,
      conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
      conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
  ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
  conn.addConnectionFactory(connFactory);
  if(Shell.WINDOWS) {
    // result of setting the SO_REUSEADDR flag is different on Windows
    // http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
    // without this 2 NN's can start on the same machine and listen on
    // the same port with indeterminate routing of incoming requests to them
    conn.setReuseAddress(false);
  }
  return conn;
}
 
Example 7
Source File: JettyWebServer.java    From oxygen with Apache License 2.0 5 votes vote down vote up
private void configConnector(ServerConnector connector, JettyConf jettyConf) {
  connector.setPort(this.port);
  connector.setIdleTimeout(jettyConf.getIdleTimeout());
  connector.setAcceptQueueSize(jettyConf.getAcceptQueueSize());
  connector.setStopTimeout(jettyConf.getStopTimeout());
  connector.setReuseAddress(jettyConf.isReuseAddress());
}
 
Example 8
Source File: ConnectorFactory.java    From vespa with Apache License 2.0 5 votes vote down vote up
public ServerConnector createConnector(final Metric metric, final Server server) {
    ServerConnector connector = new JDiscServerConnector(
            connectorConfig, metric, server, createConnectionFactories(metric).toArray(ConnectionFactory[]::new));
    connector.setPort(connectorConfig.listenPort());
    connector.setName(connectorConfig.name());
    connector.setAcceptQueueSize(connectorConfig.acceptQueueSize());
    connector.setReuseAddress(connectorConfig.reuseAddress());
    connector.setIdleTimeout((long)(connectorConfig.idleTimeout() * 1000.0));
    return connector;
}
 
Example 9
Source File: JettyServer.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public final void run() throws Exception {
    final org.eclipse.jetty.server.Server s = new org.eclipse.jetty.server.Server(new QueuedThreadPool(200, Runtime.getRuntime().availableProcessors()));

    final ServerConnector http = new ServerConnector(s);
    http.setReuseAddress(true);
    http.setAcceptQueueSize(100000);
    http.setPort(8080);
    s.addConnector(http);

    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");

    final ServletHolder holder1 = new ServletHolder(new PlaintextServlet());
    context.addServlet(holder1, "/plaintext");
    holder1.setAsyncSupported(true);
    final ServletHolder holder2 = new ServletHolder(new JsonServlet());
    context.addServlet(holder2, "/json");
    holder2.setAsyncSupported(true);

    s.setHandler(context);

    s.start();
    System.err.println("Server is up.");

    AbstractEmbeddedServer.waitUrlAvailable("http://localhost:8080/plaintext");
    AbstractEmbeddedServer.waitUrlAvailable("http://localhost:8080/json");
    System.err.println("Server test cases are instrumented and bootstrapped.");

    s.join();
}
 
Example 10
Source File: OAuth2MockEndpointHolder.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private OAuth2MockEndpointHolder(final Map<String, OAuth2MockEndpoint> endpoints,
                                 final String keyStorePath,
                                 final String keyStorePassword,
                                 final String keyStoreType) throws IOException
{
    _endpoints = endpoints;
    final List<String> protocolWhiteList =
            getSystemPropertyAsList(CommonProperties.QPID_SECURITY_TLS_PROTOCOL_WHITE_LIST,
                                    CommonProperties.QPID_SECURITY_TLS_PROTOCOL_WHITE_LIST_DEFAULT);
    final List<String> protocolBlackList =
            getSystemPropertyAsList(CommonProperties.QPID_SECURITY_TLS_PROTOCOL_BLACK_LIST,
                                    CommonProperties.QPID_SECURITY_TLS_PROTOCOL_BLACK_LIST_DEFAULT);
    final List<String> cipherSuiteWhiteList =
            getSystemPropertyAsList(CommonProperties.QPID_SECURITY_TLS_CIPHER_SUITE_WHITE_LIST,
                                    CommonProperties.QPID_SECURITY_TLS_CIPHER_SUITE_WHITE_LIST_DEFAULT);
    final List<String> cipherSuiteBlackList =
            getSystemPropertyAsList(CommonProperties.QPID_SECURITY_TLS_CIPHER_SUITE_BLACK_LIST,
                                    CommonProperties.QPID_SECURITY_TLS_CIPHER_SUITE_BLACK_LIST_DEFAULT);

    _server = new Server();
    SslContextFactory.Server sslContextFactory = new SslContextFactory.Server()
                                          {
                                              @Override
                                              public void customize(final SSLEngine sslEngine)
                                              {
                                                  super.customize(sslEngine);
                                                  SSLUtil.updateEnabledCipherSuites(sslEngine, cipherSuiteWhiteList, cipherSuiteBlackList);
                                                  SSLUtil.updateEnabledTlsProtocols(sslEngine, protocolWhiteList, protocolBlackList);
                                              }
                                          };
    sslContextFactory.setKeyStorePassword(keyStorePassword);
    sslContextFactory.setKeyStoreResource(Resource.newResource(keyStorePath));
    sslContextFactory.setKeyStoreType(keyStoreType);

    // override default jetty excludes as valid IBM JDK are excluded
    // causing SSL handshake failure (due to default exclude '^SSL_.*$')
    sslContextFactory.setExcludeCipherSuites("^.*_(MD5|SHA|SHA1)$",
                                             "^TLS_RSA_.*$",
                                             "^SSL_RSA_.*$",
                                             "^.*_NULL_.*$",
                                             "^.*_anon_.*$");

    _connector = new ServerConnector(_server, sslContextFactory);
    _connector.setPort(0);
    _connector.setReuseAddress(true);
    _server.setHandler(new AbstractHandler()
    {
        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                           HttpServletResponse response) throws IOException,
                                                                ServletException
        {
            baseRequest.setHandled(true);

            try
            {
                final OAuth2MockEndpoint
                        mockEndpoint = _endpoints.get(request.getPathInfo());
                TestCase.assertNotNull(String.format("Could not find mock endpoint for request path '%s'",
                                                     request.getPathInfo()), mockEndpoint);
                if (mockEndpoint != null)
                {
                    mockEndpoint.handleRequest(request, response);
                }
            }
            catch (Throwable t)
            {
                response.setStatus(500);
                response.getOutputStream().write(String.format("{\"error\":\"test failure\",\"error_description\":\"%s\"}", t)
                                                       .getBytes(OAuth2AuthenticationProviderImplTest.UTF8));
            }
        }
    });
    _server.addConnector(_connector);
}
 
Example 11
Source File: DataServer.java    From dawnsci with Eclipse Public License 1.0 4 votes vote down vote up
public void start(boolean block) throws Exception {
	
	this.server = new Server();
	ServerConnector connector = new ServerConnector(server);
	connector.setPort(getPort());
	connector.setReuseAddress(true);
	server.addConnector(connector);   
	
	// We enable sessions on the server so that 
	// we can cache LoaderFactories to a given session.
	// The loader factory therefore needs a non-global 
	// data soft reference cache.
	ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
	context.setContextPath("/");
	server.setHandler(context);
     
       // Make individual servlets
       // Slicing (large data in binary http)
	ServletHolder holderSlice = new ServletHolder("slice", SliceServlet.class);
	context.addServlet(holderSlice, "/slice/*");
	
	// Doing events, like data changing shape.
	// FIXME Should not be needed
	WebSocketHandler wsHandler = new WebSocketHandler() {
		@Override
		public void configure(WebSocketServletFactory factory) {
			factory.register(FileMonitorSocket.class);
		}
	};
	context.setHandler(wsHandler);
	// FIXME End should not be needed.		
	
	ServletHolder holderInfo = new ServletHolder("info", InfoServlet.class);
	context.addServlet(holderInfo, "/info/*");
	
	ServletHolder holderTree = new ServletHolder("tree", TreeServlet.class);
	context.addServlet(holderTree, "/tree/*");

    
	// Events json objects to notify of problems.
	ServletHolder holderEvent = new ServletHolder("event", EventServlet.class);
	context.addServlet(holderEvent, "/event/*");
	
    server.start();
    if (block) server.join();

}