Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 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.nio;
 
 
 
 /* ------------------------------------------------------------------------------- */
Selecting NIO connector.

This connector uses efficient NIO buffers with a non blocking threading model. Direct NIO buffers are used and threads are only allocated to connections with requests. Synchronization is used to simulate blocking for the servlet API, and any unflushed content at the end of request handling is written asynchronously.

This connector is best used when there are a many connections that have idle periods.

When used with org.eclipse.jetty.continuation.Continuation, threadless waits are supported. If a filter or servlet returns after calling org.eclipse.jetty.continuation.Continuation.suspend() or when a runtime exception is thrown from a call to org.eclipse.jetty.continuation.Continuation.undispatch(), Jetty will will not send a response to the client. Instead the thread is released and the Continuation is placed on the timer queue. If the Continuation timeout expires, or it's resume method is called, then the request is again allocated a thread and the request is retried. The limitation of this approach is that request content is not available on the retried request, thus if possible it should be read after the continuation or saved as a request attribute or as the associated object of the Continuation instance.

Org.apache.xbean.XBean:
element="nioConnector" description="Creates an NIO based socket connector"
 
 {
     protected ServerSocketChannel _acceptChannel;
     private int _lowResourcesConnections;
     private int _lowResourcesMaxIdleTime;
     private int _localPort=-1;
 
     private final SelectorManager _manager = new ConnectorSelectorManager();
 
     /* ------------------------------------------------------------------------------- */
    
Constructor.
 
     public SelectChannelConnector()
     {
         addBean(,true);
         setAcceptors(Math.max(1,(Runtime.getRuntime().availableProcessors()+3)/4));
     }
     
     @Override
     public void setThreadPool(ThreadPool pool)
     {
         super.setThreadPool(pool);
         // preserve start order
         removeBean();
         addBean(,true);
     }
     
     /* ------------------------------------------------------------ */
     @Override
     public void accept(int acceptorIDthrows IOException
    {
        ServerSocketChannel server;
        synchronized(this)
        {
            server = ;
        }
        if (server!=null && server.isOpen() && .isStarted())
        {
            SocketChannel channel = server.accept();
            channel.configureBlocking(false);
            Socket socket = channel.socket();
            configure(socket);
            .register(channel);
        }
    }
    /* ------------------------------------------------------------ */
    public void close() throws IOException
    {
        synchronized(this)
        {
            if ( != null)
            {
                removeBean();
                if (.isOpen())
                    .close();
            }
             = null;
            =-2;
        }
    }
    /* ------------------------------------------------------------------------------- */
    @Override
    public void customize(EndPoint endpointRequest requestthrows IOException
    {
        request.setTimeStamp(System.currentTimeMillis());
        endpoint.setMaxIdleTime();
        super.customize(endpointrequest);
    }
    /* ------------------------------------------------------------------------------- */
    @Override
    public void persist(EndPoint endpointthrows IOException
    {
        AsyncEndPoint aEndp = ((AsyncEndPoint)endpoint);
        aEndp.setCheckForIdle(true);
        super.persist(endpoint);
    }
    /* ------------------------------------------------------------ */
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public synchronized Object getConnection()
    {
        return ;
    }
    /* ------------------------------------------------------------------------------- */
    public int getLocalPort()
    {
        synchronized(this)
        {
            return ;
        }
    }
    /* ------------------------------------------------------------ */
    public void open() throws IOException
    {
        synchronized(this)
        {
            if ( == null)
            {
                // Create a new server socket
                 = ServerSocketChannel.open();
                // Set to blocking mode
                .configureBlocking(true);
                // Bind the server socket to the local host and port
                .socket().setReuseAddress(getReuseAddress());
                InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort());
                .socket().bind(addr,getAcceptQueueSize());
                =.socket().getLocalPort();
                if (<=0)
                    throw new IOException("Server channel not bound");
                addBean();
            }
        }
    }
    /* ------------------------------------------------------------ */
    @Override
    public void setMaxIdleTime(int maxIdleTime)
    {
        .setMaxIdleTime(maxIdleTime);
        super.setMaxIdleTime(maxIdleTime);
    }
    /* ------------------------------------------------------------ */
    

Returns:
the lowResourcesConnections
    public int getLowResourcesConnections()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    
Set the number of connections, which if exceeded places this manager in low resources state. This is not an exact measure as the connection count is averaged over the select sets.

Parameters:
lowResourcesConnections the number of connections
See also:
setLowResourcesMaxIdleTime(int)
    public void setLowResourcesConnections(int lowResourcesConnections)
    {
        =lowResourcesConnections;
    }
    /* ------------------------------------------------------------ */
    

Returns:
the lowResourcesMaxIdleTime
    @Override
    public int getLowResourcesMaxIdleTime()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    
Set the period in ms that a connection is allowed to be idle when this there are more than getLowResourcesConnections() connections. This allows the server to rapidly close idle connections in order to gracefully handle high load situations.

Parameters:
lowResourcesMaxIdleTime the period in ms that a connection is allowed to be idle when resources are low.
See also:
setMaxIdleTime(int)
    @Override
    public void setLowResourcesMaxIdleTime(int lowResourcesMaxIdleTime)
    {
        =lowResourcesMaxIdleTime;
        super.setLowResourcesMaxIdleTime(lowResourcesMaxIdleTime);
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.jetty.server.server.AbstractConnector#doStart()
     */
    @Override
    protected void doStart() throws Exception
    {
        super.doStart();
    }
    /* ------------------------------------------------------------ */
    protected SelectChannelEndPoint newEndPoint(SocketChannel channelSelectSet selectSetSelectionKey keythrows IOException
    {
        SelectChannelEndPoint endpnew SelectChannelEndPoint(channel,selectSet,keySelectChannelConnector.this.);
        endp.setConnection(selectSet.getManager().newConnection(channel,endpkey.attachment()));
        return endp;
    }
    /* ------------------------------------------------------------------------------- */
    protected void endPointClosed(SelectChannelEndPoint endpoint)
    {
        connectionClosed(endpoint.getConnection());
    }
    /* ------------------------------------------------------------------------------- */
    protected AsyncConnection newConnection(SocketChannel channel,final AsyncEndPoint endpoint)
    {
        return new AsyncHttpConnection(SelectChannelConnector.this,endpoint,getServer());
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private final class ConnectorSelectorManager extends SelectorManager
    {
        @Override
        public boolean dispatch(Runnable task)
        {
            ThreadPool pool=getThreadPool();
            if (pool==null)
                pool=getServer().getThreadPool();
            return pool.dispatch(task);
        }
        @Override
        protected void endPointClosed(final SelectChannelEndPoint endpoint)
        {
            SelectChannelConnector.this.endPointClosed(endpoint);
        }
        @Override
        protected void endPointOpened(SelectChannelEndPoint endpoint)
        {
            // TODO handle max connections and low resources
            connectionOpened(endpoint.getConnection());
        }
        @Override
        protected void endPointUpgraded(ConnectedEndPoint endpointConnection oldConnection)
        {
            connectionUpgraded(oldConnection,endpoint.getConnection());
        }
        @Override
        public AsyncConnection newConnection(SocketChannel channel,AsyncEndPoint endpointObject attachment)
        {
            return SelectChannelConnector.this.newConnection(channel,endpoint);
        }
        @Override
        protected SelectChannelEndPoint newEndPoint(SocketChannel channelSelectSet selectSetSelectionKey sKeythrows IOException
        {
            return SelectChannelConnector.this.newEndPoint(channel,selectSet,sKey);
        }
    }
New to GrepCode? Check out our FAQ X