All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.server.ServerConnector Maven / Gradle / Ivy

The newest version!
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.channels.Channel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.SelectChannelEndPoint;
import org.eclipse.jetty.io.SelectorManager;
import org.eclipse.jetty.io.SelectorManager.ManagedSelector;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.Name;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.Scheduler;

/**
 * This {@link Connector} implementation is the primary connector for the
 * Jetty server over TCP/IP.  By the use of various {@link ConnectionFactory} instances it is able
 * to accept connections for HTTP, SPDY and WebSocket, either directly or over SSL.
 * 

* The connector is a fully asynchronous NIO based implementation that by default will * use all the commons services (eg {@link Executor}, {@link Scheduler}) of the * passed {@link Server} instance, but all services may also be constructor injected * into the connector so that it may operate with dedicated or otherwise shared services. *

*

Connection Factories

* Various convenience constructors are provided to assist with common configurations of * ConnectionFactories, whose generic use is described in {@link AbstractConnector}. * If no connection factories are passed, then the connector will * default to use a {@link HttpConnectionFactory}. If an non null {@link SslContextFactory} * instance is passed, then this used to instantiate a {@link SslConnectionFactory} which is * prepended to the other passed or default factories. *

*

Selectors

* The connector will use the {@link Executor} service to execute a number of Selector Tasks, * which are implemented to each use a NIO {@link Selector} instance to asynchronously * schedule a set of accepted connections. It is the selector thread that will call the * {@link Callback} instances passed in the {@link EndPoint#fillInterested(Object, Callback)} or * {@link EndPoint#write(Object, Callback, java.nio.ByteBuffer...)} methods. It is expected * that these callbacks may do some non-blocking IO work, but will always dispatch to the * {@link Executor} service any blocking, long running or application tasks. *

* The default number of selectors is equal to the number of processors available to the JVM, * which should allow optimal performance even if all the connections used are performing * significant non-blocking work in the callback tasks. * */ @ManagedObject("HTTP connector using NIO ByteChannels and Selectors") public class ServerConnector extends AbstractNetworkConnector { private final SelectorManager _manager; private volatile ServerSocketChannel _acceptChannel; private volatile boolean _inheritChannel = false; private volatile int _localPort = -1; private volatile int _acceptQueueSize = 0; private volatile boolean _reuseAddress = true; private volatile int _lingerTime = -1; /* ------------------------------------------------------------ */ /** HTTP Server Connection. *

Construct a ServerConnector with a private instance of {@link HttpConnectionFactory} as the only factory.

* @param server The {@link Server} this connector will accept connection for. */ public ServerConnector( @Name("server") Server server) { this(server,null,null,null,0,0,new HttpConnectionFactory()); } /* ------------------------------------------------------------ */ /** Generic Server Connection with default configuration. *

Construct a Server Connector with the passed Connection factories.

* @param server The {@link Server} this connector will accept connection for. * @param factories Zero or more {@link ConnectionFactory} instances used to create and configure connections. */ public ServerConnector( @Name("server") Server server, @Name("factories") ConnectionFactory... factories) { this(server,null,null,null,0,0,factories); } /* ------------------------------------------------------------ */ /** HTTP Server Connection. *

Construct a ServerConnector with a private instance of {@link HttpConnectionFactory} as the primary protocol

. * @param server The {@link Server} this connector will accept connection for. * @param sslContextFactory If non null, then a {@link SslConnectionFactory} is instantiated and prepended to the * list of HTTP Connection Factory. */ public ServerConnector( @Name("server") Server server, @Name("sslContextFactory") SslContextFactory sslContextFactory) { this(server,null,null,null,0,0,AbstractConnectionFactory.getFactories(sslContextFactory,new HttpConnectionFactory())); } /* ------------------------------------------------------------ */ /** Generic SSL Server Connection. * @param server The {@link Server} this connector will accept connection for. * @param sslContextFactory If non null, then a {@link SslConnectionFactory} is instantiated and prepended to the * list of ConnectionFactories, with the first factory being the default protocol for the SslConnectionFactory. * @param factories Zero or more {@link ConnectionFactory} instances used to create and configure connections. */ public ServerConnector( @Name("server") Server server, @Name("sslContextFactory") SslContextFactory sslContextFactory, @Name("factories") ConnectionFactory... factories) { this(server,null,null,null,0,0,AbstractConnectionFactory.getFactories(sslContextFactory,factories)); } /** Generic Server Connection. * @param server * The server this connector will be accept connection for. * @param executor * An executor used to run tasks for handling requests, acceptors and selectors. I * If null then use the servers executor * @param scheduler * A scheduler used to schedule timeouts. If null then use the servers scheduler * @param bufferPool * A ByteBuffer pool used to allocate buffers. If null then create a private pool with default configuration. * @param acceptors * the number of acceptor threads to use, or 0 for a default value. Acceptors accept new TCP/IP connections. * @param selectors * the number of selector threads, or 0 for a default value. Selectors notice and schedule established connection that can make IO progress. * @param factories * Zero or more {@link ConnectionFactory} instances used to create and configure connections. */ public ServerConnector( @Name("server") Server server, @Name("executor") Executor executor, @Name("scheduler") Scheduler scheduler, @Name("bufferPool") ByteBufferPool bufferPool, @Name("acceptors") int acceptors, @Name("selectors") int selectors, @Name("factories") ConnectionFactory... factories) { super(server,executor,scheduler,bufferPool,acceptors,factories); _manager = new ServerConnectorManager(getExecutor(), getScheduler(), selectors > 0 ? selectors : Runtime.getRuntime().availableProcessors()); addBean(_manager, true); } @Override public boolean isOpen() { ServerSocketChannel channel = _acceptChannel; return channel!=null && channel.isOpen(); } /** * @return whether this connector uses a channel inherited from the JVM. * @see System#inheritedChannel() */ public boolean isInheritChannel() { return _inheritChannel; } /** *

Sets whether this connector uses a channel inherited from the JVM.

*

If true, the connector first tries to inherit from a channel provided by the system. * If there is no inherited channel available, or if the inherited channel is not usable, * then it will fall back using {@link ServerSocketChannel}.

*

Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port * used to access pages on the Jetty instance is the same as the port used to * launch Jetty.

* * @param inheritChannel whether this connector uses a channel inherited from the JVM. */ public void setInheritChannel(boolean inheritChannel) { _inheritChannel = inheritChannel; } @Override public void open() throws IOException { if (_acceptChannel == null) { ServerSocketChannel serverChannel = null; if (isInheritChannel()) { Channel channel = System.inheritedChannel(); if (channel instanceof ServerSocketChannel) serverChannel = (ServerSocketChannel)channel; else LOG.warn("Unable to use System.inheritedChannel() [{}]. Trying a new ServerSocketChannel at {}:{}", channel, getHost(), getPort()); } if (serverChannel == null) { serverChannel = ServerSocketChannel.open(); InetSocketAddress bindAddress = getHost() == null ? new InetSocketAddress(getPort()) : new InetSocketAddress(getHost(), getPort()); serverChannel.socket().bind(bindAddress, getAcceptQueueSize()); serverChannel.socket().setReuseAddress(getReuseAddress()); _localPort = serverChannel.socket().getLocalPort(); if (_localPort <= 0) throw new IOException("Server channel not bound"); addBean(serverChannel); } serverChannel.configureBlocking(true); addBean(serverChannel); _acceptChannel = serverChannel; } } @Override public Future shutdown(C c) { // TODO shutdown all the connections return super.shutdown(c); } @Override public void close() { ServerSocketChannel serverChannel = _acceptChannel; _acceptChannel = null; if (serverChannel != null) { removeBean(serverChannel); // If the interrupt did not close it, we should close it if (serverChannel.isOpen()) { try { serverChannel.close(); } catch (IOException e) { LOG.warn(e); } } } // super.close(); _localPort = -2; } @Override public void accept(int acceptorID) throws IOException { ServerSocketChannel serverChannel = _acceptChannel; if (serverChannel != null && serverChannel.isOpen()) { SocketChannel channel = serverChannel.accept(); channel.configureBlocking(false); Socket socket = channel.socket(); configure(socket); _manager.accept(channel); } } protected void configure(Socket socket) { try { socket.setTcpNoDelay(true); if (_lingerTime >= 0) socket.setSoLinger(true, _lingerTime / 1000); else socket.setSoLinger(false, 0); } catch (SocketException e) { LOG.ignore(e); } } public SelectorManager getSelectorManager() { return _manager; } @Override public Object getTransport() { return _acceptChannel; } @Override @ManagedAttribute("local port") public int getLocalPort() { return _localPort; } protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException { return new SelectChannelEndPoint(channel, selectSet, key, getScheduler(), getIdleTimeout()); } /** * @return the linger time * @see Socket#getSoLinger() */ @ManagedAttribute("TCP/IP solinger time or -1 to disable") public int getSoLingerTime() { return _lingerTime; } /** * @param lingerTime the linger time. Use -1 to disable. * @see Socket#setSoLinger(boolean, int) */ public void setSoLingerTime(int lingerTime) { _lingerTime = lingerTime; } /** * @return the accept queue size */ @ManagedAttribute("Accept Queue size") public int getAcceptQueueSize() { return _acceptQueueSize; } /** * @param acceptQueueSize the accept queue size (also known as accept backlog) */ public void setAcceptQueueSize(int acceptQueueSize) { _acceptQueueSize = acceptQueueSize; } /** * @return whether the server socket reuses addresses * @see ServerSocket#getReuseAddress() */ public boolean getReuseAddress() { return _reuseAddress; } /** * @param reuseAddress whether the server socket reuses addresses * @see ServerSocket#setReuseAddress(boolean) */ public void setReuseAddress(boolean reuseAddress) { _reuseAddress = reuseAddress; } private final class ServerConnectorManager extends SelectorManager { private ServerConnectorManager(Executor executor, Scheduler scheduler, int selectors) { super(executor, scheduler, selectors); } @Override protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey selectionKey) throws IOException { return ServerConnector.this.newEndPoint(channel, selectSet, selectionKey); } @Override public Connection newConnection(SocketChannel channel, EndPoint endpoint, Object attachment) throws IOException { return getDefaultConnectionFactory().newConnection(ServerConnector.this, endpoint); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy