Class AbstractIOReactor
- java.lang.Object
-
- org.apache.http.impl.nio.reactor.AbstractIOReactor
-
- All Implemented Interfaces:
IOReactor
- Direct Known Subclasses:
BaseIOReactor
public abstract class AbstractIOReactor extends java.lang.Object implements IOReactor
Generic implementation ofIOReactor
that can used as a subclass for more specialized I/O reactors. It is based on a singleSelector
instance.- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<IOSession>
closedSessions
private java.util.Queue<InterestOpEntry>
interestOpsQueue
private boolean
interestOpsQueueing
private java.util.Queue<ChannelEntry>
newChannels
private java.nio.channels.Selector
selector
private long
selectTimeout
private java.util.Set<IOSession>
sessions
private IOReactorStatus
status
private java.lang.Object
statusMutex
-
Constructor Summary
Constructors Constructor Description AbstractIOReactor(long selectTimeout)
Creates new AbstractIOReactor instance.AbstractIOReactor(long selectTimeout, boolean interestOpsQueueing)
Creates new AbstractIOReactor instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
acceptable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_ACCEPT
readiness.void
addChannel(ChannelEntry channelEntry)
Adds new channel entry.void
awaitShutdown(long timeout)
Blocks for the given period of time in milliseconds awaiting the completion of the reactor shutdown.protected void
closeActiveChannels()
Closes out all active channels registered with the selector of this I/O reactor.protected void
closeNewChannels()
Closes out all new channels pending registration with the selector of this I/O reactor.protected void
closeSessions()
Closes out all I/O sessions maintained by this I/O reactor.protected abstract void
connectable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_CONNECT
readiness.protected void
execute()
Activates the I/O reactor.boolean
getInterestOpsQueueing()
Returnstrue
if interest Ops queueing is enabled,false
otherwise.protected IOSession
getSession(java.nio.channels.SelectionKey key)
ObtainsIOSession
instance associated with the given selection key.IOReactorStatus
getStatus()
Returns the current status of the reactor.void
gracefulShutdown()
Attempts graceful shutdown of this I/O reactor.void
hardShutdown()
Attempts force-shutdown of this I/O reactor.private void
processClosedSessions()
protected void
processEvent(java.nio.channels.SelectionKey key)
Processes new event on the given selection key.private void
processEvents(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)
private void
processNewChannels()
private void
processPendingInterestOps()
protected void
queueClosedSession(IOSession session)
Queues the given I/O session to be processed asynchronously as closed.private boolean
queueInterestOps(InterestOpEntry entry)
protected abstract void
readable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_READ
readiness.protected void
sessionClosed(IOSession session)
Triggered when a session has been closed.protected void
sessionCreated(java.nio.channels.SelectionKey key, IOSession session)
Triggered when new session has been created.protected void
sessionTimedOut(IOSession session)
Triggered when a session has timed out.void
shutdown()
Initiates shutdown of the reactor and blocks for a default period of time waiting for the reactor to terminate all active connections, to shut down itself and to release system resources it currently holds.void
shutdown(long gracePeriod)
Initiates shutdown of the reactor and blocks approximately for the given period of time in milliseconds waiting for the reactor to terminate all active connections, to shut down itself and to release system resources it currently holds.protected void
timeoutCheck(java.nio.channels.SelectionKey key, long now)
Triggered to verify whether the I/O session associated with the given selection key has not timed out.protected abstract void
validate(java.util.Set<java.nio.channels.SelectionKey> keys)
Triggered to validate keys currently registered with the selector.protected abstract void
writable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_WRITE
readiness.
-
-
-
Field Detail
-
status
private volatile IOReactorStatus status
-
statusMutex
private final java.lang.Object statusMutex
-
selectTimeout
private final long selectTimeout
-
interestOpsQueueing
private final boolean interestOpsQueueing
-
selector
private final java.nio.channels.Selector selector
-
sessions
private final java.util.Set<IOSession> sessions
-
interestOpsQueue
private final java.util.Queue<InterestOpEntry> interestOpsQueue
-
closedSessions
private final java.util.Queue<IOSession> closedSessions
-
newChannels
private final java.util.Queue<ChannelEntry> newChannels
-
-
Constructor Detail
-
AbstractIOReactor
public AbstractIOReactor(long selectTimeout) throws IOReactorException
Creates new AbstractIOReactor instance.- Parameters:
selectTimeout
- the select timeout.- Throws:
IOReactorException
- in case if a non-recoverable I/O error.
-
AbstractIOReactor
public AbstractIOReactor(long selectTimeout, boolean interestOpsQueueing) throws IOReactorException
Creates new AbstractIOReactor instance.- Parameters:
selectTimeout
- the select timeout.interestOpsQueueing
- Ops queueing flag.- Throws:
IOReactorException
- in case if a non-recoverable I/O error.- Since:
- 4.1
-
-
Method Detail
-
acceptable
protected abstract void acceptable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_ACCEPT
readiness.Super-classes can implement this method to react to the event.
- Parameters:
key
- the selection key.
-
connectable
protected abstract void connectable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_CONNECT
readiness.Super-classes can implement this method to react to the event.
- Parameters:
key
- the selection key.
-
readable
protected abstract void readable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_READ
readiness.Super-classes can implement this method to react to the event.
- Parameters:
key
- the selection key.
-
writable
protected abstract void writable(java.nio.channels.SelectionKey key)
Triggered when the key signalsSelectionKey.OP_WRITE
readiness.Super-classes can implement this method to react to the event.
- Parameters:
key
- the selection key.
-
validate
protected abstract void validate(java.util.Set<java.nio.channels.SelectionKey> keys)
Triggered to validate keys currently registered with the selector. This method is called after each I/O select loop.Super-classes can implement this method to run validity checks on active sessions and include additional processing that needs to be executed after each I/O select loop.
- Parameters:
keys
- all selection keys registered with the selector.
-
sessionCreated
protected void sessionCreated(java.nio.channels.SelectionKey key, IOSession session)
Triggered when new session has been created.Super-classes can implement this method to react to the event.
- Parameters:
key
- the selection key.session
- new I/O session.
-
sessionClosed
protected void sessionClosed(IOSession session)
Triggered when a session has been closed.Super-classes can implement this method to react to the event.
- Parameters:
session
- closed I/O session.
-
sessionTimedOut
protected void sessionTimedOut(IOSession session)
Triggered when a session has timed out.Super-classes can implement this method to react to the event.
- Parameters:
session
- timed out I/O session.
-
getSession
protected IOSession getSession(java.nio.channels.SelectionKey key)
ObtainsIOSession
instance associated with the given selection key.- Parameters:
key
- the selection key.- Returns:
- I/O session.
-
getStatus
public IOReactorStatus getStatus()
Description copied from interface:IOReactor
Returns the current status of the reactor.
-
getInterestOpsQueueing
public boolean getInterestOpsQueueing()
Returnstrue
if interest Ops queueing is enabled,false
otherwise.- Since:
- 4.1
-
addChannel
public void addChannel(ChannelEntry channelEntry)
Adds new channel entry. The channel will be asynchronously registered with the selector.- Parameters:
channelEntry
- the channel entry.
-
execute
protected void execute() throws java.io.InterruptedIOException, IOReactorException
Activates the I/O reactor. The I/O reactor will start reacting to I/O events and triggering notification methods.This method will enter the infinite I/O select loop on the
Selector
instance associated with this I/O reactor.The method will remain blocked unto the I/O reactor is shut down or the execution thread is interrupted.
- Throws:
java.io.InterruptedIOException
- if the dispatch thread is interrupted.IOReactorException
- in case if a non-recoverable I/O error.- See Also:
acceptable(SelectionKey)
,connectable(SelectionKey)
,readable(SelectionKey)
,writable(SelectionKey)
,timeoutCheck(SelectionKey, long)
,validate(Set)
,sessionCreated(SelectionKey, IOSession)
,sessionClosed(IOSession)
-
processEvents
private void processEvents(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)
-
processEvent
protected void processEvent(java.nio.channels.SelectionKey key)
Processes new event on the given selection key.- Parameters:
key
- the selection key that triggered an event.
-
queueClosedSession
protected void queueClosedSession(IOSession session)
Queues the given I/O session to be processed asynchronously as closed.- Parameters:
session
- the closed I/O session.
-
processNewChannels
private void processNewChannels() throws IOReactorException
- Throws:
IOReactorException
-
processClosedSessions
private void processClosedSessions()
-
processPendingInterestOps
private void processPendingInterestOps()
-
queueInterestOps
private boolean queueInterestOps(InterestOpEntry entry)
-
timeoutCheck
protected void timeoutCheck(java.nio.channels.SelectionKey key, long now)
Triggered to verify whether the I/O session associated with the given selection key has not timed out.Super-classes can implement this method to react to the event.
- Parameters:
key
- the selection key.now
- current time as long value.
-
closeSessions
protected void closeSessions()
Closes out all I/O sessions maintained by this I/O reactor.
-
closeNewChannels
protected void closeNewChannels() throws IOReactorException
Closes out all new channels pending registration with the selector of this I/O reactor.- Throws:
IOReactorException
- - not thrown currently
-
closeActiveChannels
protected void closeActiveChannels() throws IOReactorException
Closes out all active channels registered with the selector of this I/O reactor.- Throws:
IOReactorException
- - not thrown currently
-
gracefulShutdown
public void gracefulShutdown()
Attempts graceful shutdown of this I/O reactor.
-
hardShutdown
public void hardShutdown() throws IOReactorException
Attempts force-shutdown of this I/O reactor.- Throws:
IOReactorException
-
awaitShutdown
public void awaitShutdown(long timeout) throws java.lang.InterruptedException
Blocks for the given period of time in milliseconds awaiting the completion of the reactor shutdown.- Parameters:
timeout
- the maximum wait time.- Throws:
java.lang.InterruptedException
- if interrupted.
-
shutdown
public void shutdown(long gracePeriod) throws IOReactorException
Description copied from interface:IOReactor
Initiates shutdown of the reactor and blocks approximately for the given period of time in milliseconds waiting for the reactor to terminate all active connections, to shut down itself and to release system resources it currently holds.- Specified by:
shutdown
in interfaceIOReactor
- Parameters:
gracePeriod
- wait time in milliseconds.- Throws:
IOReactorException
-
shutdown
public void shutdown() throws IOReactorException
Description copied from interface:IOReactor
Initiates shutdown of the reactor and blocks for a default period of time waiting for the reactor to terminate all active connections, to shut down itself and to release system resources it currently holds. It is up to individual implementations to decide for how long this method can remain blocked.- Specified by:
shutdown
in interfaceIOReactor
- Throws:
IOReactorException
-
-