|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object fulmine.AbstractLifeCycle fulmine.util.io.SelectorTasks
public final class SelectorTasks
A collection of ISelectionKeyTask
objects that handle the logic for
the ready operations raised from registered NIO Channel
objects. This
class exists to provide a mechanism to manage the tasks that should be run
for each channel that is registered against a single Selector
. Each
instance has an internal Selector
that is used to register with the
Channel
supplied in the
register(int, SelectableChannel, ISelectionKeyTask)
method.
This object invokes the Selector.select()
method and blocks until a
channel is available for one of the registered operations. When this happens
the appropriate task is located and executed.
// create and register SelectorTasks tasks = new SelectorTasks(); // create an optional listener to get notified when the selector is opened ISelectorTasksStateListener = ... tasks.setStateListener(listener); // OPEN THE CHANNEL - no other operation is valid until this is run tasks.openSelector(); SelectableChannel channel = ...; ISelectionKeyTask task = ...; tasks.register(SelectionKey.OP_READ, channel, task); // process - this blocks until a ready operation occurs, // in the real-world, this would be called in a separate thread while(tasks.process());There should only be 1 thread running the process method. The
getRunnable()
method returns an Runnable
object for a
dedicated processing thread. e.g.
// create and register SelectorTasks tasks = new SelectorTasks(); ... // kick off a thread to run the process() method new Thread(tasks.getRunnable()).start();This is not thread aware.
Constructor Summary | |
---|---|
SelectorTasks()
Standard constructor |
Method Summary | |
---|---|
protected void |
doDestroy()
Overridden in subclasses to perform custom logic on destruction. |
protected void |
doStart()
Overridden in subclasses to perform custom logic on activation. |
protected AsyncLog |
getLog()
Get the log to use for the object hierarchy |
Runnable |
getRunnable()
Get a Runnable to continually execute the process()
method. |
void |
openSelector()
Open the internal Selector |
boolean |
process()
Call Selector.select() and wait for a one of the operations in
any of the registered channels to occur. |
void |
register(int op,
SelectableChannel channel,
ISelectionKeyTask task)
Register the internal Selector with the channel and map the
supplied task to the SelectionKey that is returned from the
SelectableChannel.register(Selector, int) method. |
void |
setStateListener(ISelectorTasksStateListener listener)
Set the state listener |
ISelectionKeyTask |
unregister(SelectionKey key)
Unregister the selection key and remove the associated ISelectionKeyTask . |
Methods inherited from class fulmine.AbstractLifeCycle |
---|
checkActive, destroy, finalize, isActive, start, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface fulmine.ILifeCycle |
---|
isActive, start |
Methods inherited from interface fulmine.IDestroyable |
---|
destroy |
Constructor Detail |
---|
public SelectorTasks()
Method Detail |
---|
public void register(int op, SelectableChannel channel, ISelectionKeyTask task)
Selector
with the channel and map the
supplied task to the SelectionKey
that is returned from the
SelectableChannel.register(Selector, int)
method.
op
- the operation to register forchannel
- the channel that will register with the internal selectortask
- the task to run when the operation occurs in the channel. This
task should be efficient; if it does any blocking this affects
processing of other registered ISelectionKeyTask
objects.SelectionKey
public ISelectionKeyTask unregister(SelectionKey key)
ISelectionKeyTask
. The key is also cancelled via the
SelectionKey.cancel()
method.
key
- the selection key to unregister and cancel
public boolean process()
Selector.select()
and wait for a one of the operations in
any of the registered channels to occur. When one does, the appropriate
ISelectionKeyTask
is run.
false
if this becomes inactivepublic void openSelector()
Selector
protected AsyncLog getLog()
AbstractLifeCycle
getLog
in class AbstractLifeCycle
protected final void doStart()
AbstractLifeCycle
RuntimeException
or subclass
thereof. When this method is called, the AbstractLifeCycle.isActive()
method will
return true
.
doStart
in class AbstractLifeCycle
protected final void doDestroy()
AbstractLifeCycle
RuntimeException
or subclass
thereof.
doDestroy
in class AbstractLifeCycle
public void setStateListener(ISelectorTasksStateListener listener)
listener
- public Runnable getRunnable()
Runnable
to continually execute the process()
method.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |