Search in sources :

Example 1 with AbstractConnector

use of org.mortbay.jetty.AbstractConnector in project commons by twitter.

the class JettyHttpServerDispatch method openConnector.

/**
   * Opens a new Connector which is a Jetty specific way of handling the
   * lifecycle and configuration of the Jetty server. The connector will
   * open a Socket on an available port between minPort and maxPort.
   * A subclass can override this method to modify connector configurations
   * such as queue-size or header-buffer-size.
   * @param minPort the minimum port number to bind to.
   * @param maxPort the maximum port number to bind to.
   * @return
   */
protected Connector openConnector(int minPort, int maxPort) {
    if (minPort != 0 || maxPort != 0) {
        Preconditions.checkState(minPort > 0, "Invalid port range.");
        Preconditions.checkState(maxPort > 0, "Invalid port range.");
        Preconditions.checkState(minPort <= maxPort, "Invalid port range.");
    }
    int attempts = 0;
    int port;
    int maxAttempts = minPort == maxPort ? 1 : 5;
    while (++attempts <= maxAttempts) {
        if (minPort == maxPort) {
            port = minPort;
        } else {
            port = minPort + new Random().nextInt(maxPort - minPort);
        }
        LOG.info("Attempting to listen on port " + port);
        try {
            // TODO(John Sirois): consider making Connector impl parametrizable
            AbstractConnector connector = new SelectChannelConnector();
            connector.setPort(port);
            // Create the server with a maximum TCP backlog of 50, meaning that when the request queue
            // exceeds 50, subsequent connections may be rejected.
            connector.setAcceptQueueSize(50);
            connector.open();
            return connector;
        } catch (IOException e) {
            LOG.log(Level.WARNING, "Failed to create HTTP server on port " + port, e);
        }
    }
    return null;
}
Also used : SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) Random(java.util.Random) IOException(java.io.IOException) AbstractConnector(org.mortbay.jetty.AbstractConnector)

Aggregations

IOException (java.io.IOException)1 Random (java.util.Random)1 AbstractConnector (org.mortbay.jetty.AbstractConnector)1 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)1