public abstract class AbstractIOReactor extends Object implements IOReactor
IOReactor
that can used as a subclass
for more specialized I/O reactors. It is based on a single Selector
instance.Modifier and Type | Field and Description |
---|---|
private Queue<IOSession> |
closedSessions |
private Queue<InterestOpEntry> |
interestOpsQueue |
private boolean |
interestOpsQueueing |
private Queue<ChannelEntry> |
newChannels |
private Selector |
selector |
private long |
selectTimeout |
private Set<IOSession> |
sessions |
private IOReactorStatus |
status |
private Object |
statusMutex |
Constructor and Description |
---|
AbstractIOReactor(long selectTimeout)
Creates new AbstractIOReactor instance.
|
AbstractIOReactor(long selectTimeout,
boolean interestOpsQueueing)
Creates new AbstractIOReactor instance.
|
Modifier and Type | Method and Description |
---|---|
protected abstract void |
acceptable(SelectionKey key)
Triggered when the key signals
SelectionKey.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(SelectionKey key)
Triggered when the key signals
SelectionKey.OP_CONNECT readiness. |
protected void |
execute()
Activates the I/O reactor.
|
boolean |
getInterestOpsQueueing()
Returns
true if interest Ops queueing is enabled, false otherwise. |
protected IOSession |
getSession(SelectionKey key)
Obtains
IOSession 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(SelectionKey key)
Processes new event on the given selection key.
|
private void |
processEvents(Set<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(SelectionKey key)
Triggered when the key signals
SelectionKey.OP_READ readiness. |
protected void |
sessionClosed(IOSession session)
Triggered when a session has been closed.
|
protected void |
sessionCreated(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(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(Set<SelectionKey> keys)
Triggered to validate keys currently registered with the selector.
|
protected abstract void |
writable(SelectionKey key)
Triggered when the key signals
SelectionKey.OP_WRITE readiness. |
private volatile IOReactorStatus status
private final Object statusMutex
private final long selectTimeout
private final boolean interestOpsQueueing
private final Queue<InterestOpEntry> interestOpsQueue
private final Queue<IOSession> closedSessions
private final Queue<ChannelEntry> newChannels
public AbstractIOReactor(long selectTimeout) throws IOReactorException
selectTimeout
- the select timeout.IOReactorException
- in case if a non-recoverable I/O error.public AbstractIOReactor(long selectTimeout, boolean interestOpsQueueing) throws IOReactorException
selectTimeout
- the select timeout.interestOpsQueueing
- Ops queueing flag.IOReactorException
- in case if a non-recoverable I/O error.protected abstract void acceptable(SelectionKey key)
SelectionKey.OP_ACCEPT
readiness.
Super-classes can implement this method to react to the event.
key
- the selection key.protected abstract void connectable(SelectionKey key)
SelectionKey.OP_CONNECT
readiness.
Super-classes can implement this method to react to the event.
key
- the selection key.protected abstract void readable(SelectionKey key)
SelectionKey.OP_READ
readiness.
Super-classes can implement this method to react to the event.
key
- the selection key.protected abstract void writable(SelectionKey key)
SelectionKey.OP_WRITE
readiness.
Super-classes can implement this method to react to the event.
key
- the selection key.protected abstract void validate(Set<SelectionKey> keys)
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.
keys
- all selection keys registered with the selector.protected void sessionCreated(SelectionKey key, IOSession session)
Super-classes can implement this method to react to the event.
key
- the selection key.session
- new I/O session.protected void sessionClosed(IOSession session)
Super-classes can implement this method to react to the event.
session
- closed I/O session.protected void sessionTimedOut(IOSession session)
Super-classes can implement this method to react to the event.
session
- timed out I/O session.protected IOSession getSession(SelectionKey key)
IOSession
instance associated with the given selection
key.key
- the selection key.public IOReactorStatus getStatus()
IOReactor
public boolean getInterestOpsQueueing()
true
if interest Ops queueing is enabled, false
otherwise.public void addChannel(ChannelEntry channelEntry)
channelEntry
- the channel entry.protected void execute() throws InterruptedIOException, IOReactorException
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.
InterruptedIOException
- if the dispatch thread is interrupted.IOReactorException
- in case if a non-recoverable I/O error.acceptable(SelectionKey)
,
connectable(SelectionKey)
,
readable(SelectionKey)
,
writable(SelectionKey)
,
timeoutCheck(SelectionKey, long)
,
validate(Set)
,
sessionCreated(SelectionKey, IOSession)
,
sessionClosed(IOSession)
private void processEvents(Set<SelectionKey> selectedKeys)
protected void processEvent(SelectionKey key)
key
- the selection key that triggered an event.protected void queueClosedSession(IOSession session)
session
- the closed I/O session.private void processNewChannels() throws IOReactorException
IOReactorException
private void processClosedSessions()
private void processPendingInterestOps()
private boolean queueInterestOps(InterestOpEntry entry)
protected void timeoutCheck(SelectionKey key, long now)
Super-classes can implement this method to react to the event.
key
- the selection key.now
- current time as long value.protected void closeSessions()
protected void closeNewChannels() throws IOReactorException
IOReactorException
- - not thrown currentlyprotected void closeActiveChannels() throws IOReactorException
IOReactorException
- - not thrown currentlypublic void gracefulShutdown()
public void hardShutdown() throws IOReactorException
IOReactorException
public void awaitShutdown(long timeout) throws InterruptedException
timeout
- the maximum wait time.InterruptedException
- if interrupted.public void shutdown(long gracePeriod) throws IOReactorException
IOReactor
shutdown
in interface IOReactor
gracePeriod
- wait time in milliseconds.IOReactorException
public void shutdown() throws IOReactorException
IOReactor
shutdown
in interface IOReactor
IOReactorException
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses