Top Description Inners Fields Constructors Methods
org.mortbay.jetty.nio

public Class SelectChannelConnector

extends AbstractNIOConnector
Class Inheritance
Known Direct Subclasses
org.mortbay.jetty.nio.InheritedChannelConnector
Imports
java.io.IOException, java.net.InetSocketAddress, .Socket, java.nio.channels.SelectionKey, .ServerSocketChannel, .SocketChannel, org.mortbay.io.Connection, org.mortbay.io.nio.SelectChannelEndPoint, .SelectorManager, .SelectorManager.SelectSet, org.mortbay.jetty.HttpConnection, .Request, .RetryRequest, org.mortbay.log.Log, org.mortbay.thread.Timeout, org.mortbay.util.ajax.Continuation

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.mortbay.util.ajax.Continuation, threadless waits are supported. When a filter or servlet calls getEvent on a Continuation, a org.mortbay.jetty.RetryRequest runtime exception is thrown to allow the thread to exit the current request handling. Jetty will catch this exception and 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.

Author
gregw

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class

Field Summary

Modifier and TypeField and Description
protected transient ServerSocketChannel
private long
private long
private SelectorManager

Constructor Summary

AccessConstructor and Description
public
SelectChannelConnector()

Constructor.

Method Summary

Modifier and TypeMethod and Description
public void
accept(int acceptorID)

Implements abstract org.mortbay.jetty.AbstractConnector.accept.

public void
public void
customize(EndPoint endpoint, Request request)

Overrides org.mortbay.jetty.AbstractConnector.customize.

Implements org.mortbay.jetty.Connector.customize.

Customize a request for an endpoint.
protected void
protected void
public Object
public boolean

Returns:

Returns the assumeShortDispatch.
getDelaySelectKeyUpdate
()

Get delay select key update If true, the select set is not updated when a endpoint is dispatched for reading.

public int
public long

Returns:

the lowResourcesConnections
getLowResourcesConnections
()

public long

Returns:

the lowResourcesMaxIdleTime
getLowResourcesMaxIdleTime
()

protected Connection
public Continuation
protected SelectChannelEndPoint
public void
open()

Implements org.mortbay.jetty.Connector.open.

Opens the connector
public void
public void
setDelaySelectKeyUpdate(boolean
If true, updating a SelectionKey is delayed until a redundant event is schedules. This is an optimization that assumes event handling can be completed before the next select completes.
delay
)

public void
setLowResourceMaxIdleTime(int
the period in ms that a connection is allowed to be idle when resources are low.
lowResourcesMaxIdleTime
)

Overrides org.mortbay.jetty.AbstractConnector.setLowResourceMaxIdleTime.

Implements org.mortbay.jetty.Connector.setLowResourceMaxIdleTime.

Set the period in ms that a connection is allowed to be idle when this there are more than getLowResourcesConnections() connections.
public void
setLowResourcesConnections(long
the number of connections
lowResourcesConnections
)

Set the number of connections, which if exceeded places this manager in low resources state.

public void
setLowResourcesMaxIdleTime(long
the period in ms that a connection is allowed to be idle when resources are low.
lowResourcesMaxIdleTime
)

Set the period in ms that a connection is allowed to be idle when this there are more than getLowResourcesConnections() connections.
public void
setMaxIdleTime(int
The maxIdleTime to set.
maxIdleTime
)

Overrides org.mortbay.jetty.AbstractConnector.setMaxIdleTime.

Implements org.mortbay.jetty.Connector.setMaxIdleTime.

Set the maximum Idle time for a connection, which roughly translates to the Socket#setSoTimeout(int) call, although with NIO implementations other mechanisms may be used to implement the timeout.
Inherited from org.mortbay.jetty.nio.AbstractNIOConnector:
getUseDirectBuffersnewBuffersetUseDirectBuffers

Field Detail

_acceptChannelback to summary
protected transient ServerSocketChannel _acceptChannel
_lowResourcesConnectionsback to summary
private long _lowResourcesConnections
_lowResourcesMaxIdleTimeback to summary
private long _lowResourcesMaxIdleTime
_managerback to summary
private SelectorManager _manager

Constructor Detail

SelectChannelConnectorback to summary
public SelectChannelConnector()

Constructor.

Method Detail

acceptback to summary
public void accept(int acceptorID) throws IOException

Implements abstract org.mortbay.jetty.AbstractConnector.accept.

closeback to summary
public void close() throws IOException

Implements org.mortbay.jetty.Connector.close.

customizeback to summary
public void customize(EndPoint endpoint, Request request) throws IOException

Overrides org.mortbay.jetty.AbstractConnector.customize.

Implements org.mortbay.jetty.Connector.customize.

Doc from org.mortbay.jetty.Connector.customize.

Customize a request for an endpoint. Called on every request to allow customization of the request for the particular endpoint (eg security properties from a SSL connection).

doStartback to summary
protected void doStart() throws Exception

Overrides org.mortbay.jetty.AbstractConnector.doStart.

doStopback to summary
protected void doStop() throws Exception

Overrides org.mortbay.jetty.AbstractConnector.doStop.

getConnectionback to summary
public Object getConnection()

Implements org.mortbay.jetty.Connector.getConnection.

Returns:Object

Doc from org.mortbay.jetty.Connector.getConnection.

the underlying socket, channel, buffer etc. for the connector.

getDelaySelectKeyUpdateback to summary
public boolean getDelaySelectKeyUpdate()

Get delay select key update If true, the select set is not updated when a endpoint is dispatched for reading. The assumption is that the task will be short and thus will probably be complete before the select is tried again.

Returns:boolean

Returns the assumeShortDispatch.

getLocalPortback to summary
public int getLocalPort()

Implements org.mortbay.jetty.Connector.getLocalPort.

Returns:int

Doc from org.mortbay.jetty.Connector.getLocalPort.

The actual port the connector is listening on or -1 if there is no port or the connector is not open.

getLowResourcesConnectionsback to summary
public long getLowResourcesConnections()
Returns:long

the lowResourcesConnections

getLowResourcesMaxIdleTimeback to summary
public long getLowResourcesMaxIdleTime()
Returns:long

the lowResourcesMaxIdleTime

newConnectionback to summary
protected Connection newConnection(SocketChannel channel, SelectChannelEndPoint endpoint)
newContinuationback to summary
public Continuation newContinuation()

Overrides org.mortbay.jetty.AbstractConnector.newContinuation.

Implements org.mortbay.jetty.Connector.newContinuation.

newEndPointback to summary
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey key) throws IOException
openback to summary
public void open() throws IOException

Implements org.mortbay.jetty.Connector.open.

Doc from org.mortbay.jetty.Connector.open.

Opens the connector

persistback to summary
public void persist(EndPoint endpoint) throws IOException

Overrides org.mortbay.jetty.AbstractConnector.persist.

Implements org.mortbay.jetty.Connector.persist.

Doc from org.mortbay.jetty.Connector.persist.

Persist an endpoint. Called after every request if the connection is to remain open.

setDelaySelectKeyUpdateback to summary
public void setDelaySelectKeyUpdate(boolean delay)
Parameters
delay:boolean

If true, updating a SelectionKey is delayed until a redundant event is schedules. This is an optimization that assumes event handling can be completed before the next select completes.

setLowResourceMaxIdleTimeback to summary
public void setLowResourceMaxIdleTime(int lowResourcesMaxIdleTime)

Overrides org.mortbay.jetty.AbstractConnector.setLowResourceMaxIdleTime.

Implements org.mortbay.jetty.Connector.setLowResourceMaxIdleTime.

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:int

the period in ms that a connection is allowed to be idle when resources are low.

See Also
setMaxIdleTime(long)
setLowResourcesConnectionsback to summary
public void setLowResourcesConnections(long lowResourcesConnections)

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:long

the number of connections

See Also
setLowResourcesMaxIdleTime(long)
setLowResourcesMaxIdleTimeback to summary
public void setLowResourcesMaxIdleTime(long lowResourcesMaxIdleTime)

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:long

the period in ms that a connection is allowed to be idle when resources are low.

See Also
setMaxIdleTime(long)
setMaxIdleTimeback to summary
public void setMaxIdleTime(int maxIdleTime)

Overrides org.mortbay.jetty.AbstractConnector.setMaxIdleTime.

Implements org.mortbay.jetty.Connector.setMaxIdleTime.

Doc from org.mortbay.jetty.AbstractConnector.setMaxIdleTime.

Set the maximum Idle time for a connection, which roughly translates to the Socket#setSoTimeout(int) call, although with NIO implementations other mechanisms may be used to implement the timeout. The max idle time is applied:

  • When waiting for a new request to be received on a connection
  • When reading the headers and content of a request
  • When writing the headers and content of a response
Jetty interprets this value as the maximum time between some progress being made on the connection. So if a single byte is read or written, then the timeout (if implemented by jetty) is reset. However, in many instances, the reading/writing is delegated to the JVM, and the semantic is more strictly enforced as the maximum time a single read/write operation can take. Note, that as Jetty supports writes of memory mapped file buffers, then a write may take many 10s of seconds for large content written to a slow device.

Previously, Jetty supported separate idle timeouts and IO operation timeouts, however the expense of changing the value of soTimeout was significant, so these timeouts were merged. With the advent of NIO, it may be possible to again differentiate these values (if there is demand).

Parameters
maxIdleTime:int

The maxIdleTime to set.

org.mortbay.jetty.nio back to summary

public Class SelectChannelConnector.ConnectorEndPoint

extends SelectChannelEndPoint
Class Inheritance

Field Summary

Inherited from org.mortbay.io.nio.SelectChannelEndPoint:
_connection_dispatched_interestOps_key_manager_readBlocked_selectSet_writable_writeBlocked

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides org.mortbay.io.nio.SelectChannelEndPoint.close.

Implements org.mortbay.io.EndPoint.close.

Close any backing stream associated with the buffer
public void
undispatch()

Overrides org.mortbay.io.nio.SelectChannelEndPoint.undispatch.

Called when a dispatched thread is no longer handling the endpoint.
Inherited from org.mortbay.io.nio.SelectChannelEndPoint:
blockReadableblockWritablecancelIdledispatchflushflushgetConnectiongetSelectSetgetTimeoutTaskidleExpiredrunscheduleIdlescheduleWritesetWritabletoString

Constructor Detail

ConnectorEndPointback to summary
public ConnectorEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey key)

Method Detail

closeback to summary
public void close() throws IOException

Overrides org.mortbay.io.nio.SelectChannelEndPoint.close.

Implements org.mortbay.io.EndPoint.close.

Doc from org.mortbay.io.EndPoint.close.

Close any backing stream associated with the buffer

undispatchback to summary
public void undispatch()

Overrides org.mortbay.io.nio.SelectChannelEndPoint.undispatch.

Doc from org.mortbay.io.nio.SelectChannelEndPoint.undispatch.

Called when a dispatched thread is no longer handling the endpoint. The selection key operations are updated.

org.mortbay.jetty.nio back to summary

public Class SelectChannelConnector.RetryContinuation

extends Task
implements Continuation, Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable, org.mortbay.util.ajax.Continuation

Field Summary

Modifier and TypeField and Description
pack-priv SelectChannelEndPoint
pack-priv boolean
pack-priv Object
pack-priv boolean
pack-priv boolean
pack-priv boolean
pack-priv RetryRequest
pack-priv long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public Object
getObject()

Implements org.mortbay.util.ajax.Continuation.getObject.

Arbitrary object associated with the continuation for context.
public long
public boolean
isNew()

Implements org.mortbay.util.ajax.Continuation.isNew.

Is this a newly created Continuation.
public boolean
isPending()

Implements org.mortbay.util.ajax.Continuation.isPending.

Get the pending status?
public boolean
isResumed()

Implements org.mortbay.util.ajax.Continuation.isResumed.

Get the resumed status?
public void
reset()

Implements org.mortbay.util.ajax.Continuation.reset.

Reset the continuation.
public void
resume()

Implements org.mortbay.util.ajax.Continuation.resume.

Resume the request.
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
public void
setObject(Object
An arbitrary object to associate with the continuation
object
)

Implements org.mortbay.util.ajax.Continuation.setObject.

Arbitrary object associated with the continuation for context.
public boolean
suspend(long
A timeout of < 0 will cause an immediate return. I timeout of 0 will wait indefinitely.
timeout
)

Implements org.mortbay.util.ajax.Continuation.suspend.

Suspend handling.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public boolean
Inherited from org.mortbay.thread.Timeout.Task:
cancelexpiredgetAgegetTimestampisExpiredisScheduledreschedulescheduleschedule

Field Detail

_endPointback to summary
pack-priv SelectChannelEndPoint _endPoint
_newback to summary
pack-priv boolean _new
_objectback to summary
pack-priv Object _object
_parkedback to summary
pack-priv boolean _parked
_pendingback to summary
pack-priv boolean _pending
_resumedback to summary
pack-priv boolean _resumed
_retryback to summary
pack-priv RetryRequest _retry
_timeoutback to summary
pack-priv long _timeout

Hides org.mortbay.thread.Timeout.Task._timeout.

Constructor Detail

RetryContinuationback to summary
public RetryContinuation()

Method Detail

expireback to summary
public void expire()

Overrides org.mortbay.thread.Timeout.Task.expire.

Doc from org.mortbay.thread.Timeout.Task.expire.

Expire task. This method is called when the timeout expires. It is called in the scope of the synchronize block (on this) that sets the isExpired() state to true.

getObjectback to summary
public Object getObject()

Implements org.mortbay.util.ajax.Continuation.getObject.

Doc from org.mortbay.util.ajax.Continuation.getObject.

Arbitrary object associated with the continuation for context.

Returns:Object

An arbitrary object associated with the continuation

getTimeoutback to summary
public long getTimeout()
isNewback to summary
public boolean isNew()

Implements org.mortbay.util.ajax.Continuation.isNew.

Doc from org.mortbay.util.ajax.Continuation.isNew.

Is this a newly created Continuation.

A newly created continuation has not had getEvent(long) called on it.

Returns:boolean

True if the continuation has just been created and has not yet suspended the request.

isPendingback to summary
public boolean isPending()

Implements org.mortbay.util.ajax.Continuation.isPending.

Doc from org.mortbay.util.ajax.Continuation.isPending.

Get the pending status? A continuation is pending while the handling of a call to suspend has not completed. For blocking continuations, pending is true only during the call to suspend(long). For non-blocking continuations, pending is true until a second call to suspend(long), thus this method can be used to determine if a request is being retried.

Returns:boolean

True if the continuation is handling a call to suspend.

isResumedback to summary
public boolean isResumed()

Implements org.mortbay.util.ajax.Continuation.isResumed.

Doc from org.mortbay.util.ajax.Continuation.isResumed.

Get the resumed status?

Returns:boolean

True if the continuation is has been resumed.

resetback to summary
public void reset()

Implements org.mortbay.util.ajax.Continuation.reset.

Doc from org.mortbay.util.ajax.Continuation.reset.

Reset the continuation. Cancel any pending status of the continuation.

resumeback to summary
public void resume()

Implements org.mortbay.util.ajax.Continuation.resume.

Doc from org.mortbay.util.ajax.Continuation.resume.

Resume the request. Resume a suspended request. The passed event will be returned in the getObject method.

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

setObjectback to summary
public void setObject(Object object)

Implements org.mortbay.util.ajax.Continuation.setObject.

Doc from org.mortbay.util.ajax.Continuation.setObject.

Arbitrary object associated with the continuation for context.

Parameters
object:Object

An arbitrary object to associate with the continuation

suspendback to summary
public boolean suspend(long timeout)

Implements org.mortbay.util.ajax.Continuation.suspend.

Doc from org.mortbay.util.ajax.Continuation.suspend.

Suspend handling. This method will suspend the request for the timeout or until resume is called.

Parameters
timeout:long

A timeout of < 0 will cause an immediate return. I timeout of 0 will wait indefinitely.

Returns:boolean

True if resume called or false if timeout.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

undispatchback to summary
public boolean undispatch()