public interface Reactor
BaseHandler
and only handle the events that it cares to
implement methods for.
This class is not thread safe (with the exception of the wakeup()
method) and should only be used by a single thread at any given time.
Modifier and Type | Interface and Description |
---|---|
static class |
Reactor.Factory |
Modifier and Type | Method and Description |
---|---|
Acceptor |
acceptor(String host,
int port)
Creates a new acceptor.
|
Acceptor |
acceptor(String host,
int port,
Handler handler)
Creates a new acceptor.
|
Record |
attachments() |
Set<ReactorChild> |
children() |
Collector |
collector() |
Connection |
connection(Handler handler)
Creates a new out-bound connection.
|
void |
free()
Frees any resources (such as sockets and selectors) held by the reactor
or its children.
|
Handler |
getGlobalHandler() |
Handler |
getHandler() |
long |
getTimeout() |
long |
mark()
Updates the last time that the reactor's state has changed, potentially
resulting in events being generated.
|
long |
now() |
boolean |
process()
Process any events pending for this reactor.
|
boolean |
quiesced() |
void |
run()
Simplifies the use of the reactor by wrapping the use of
start , run , and stop method
calls. |
Task |
schedule(int delay,
Handler handler)
Schedules execution of a task to take place at some point in the future.
|
Selectable |
selectable()
Creates a new
Selectable as a child of this reactor. |
void |
setGlobalHandler(Handler handler)
Sets a new global handler.
|
void |
setHandler(Handler handler)
Sets a new handler, that will receive any events not handled by a child
of the reactor.
|
void |
setTimeout(long timeout)
The value the reactor will use for
Selector.select(long) that is called as part of process() . |
void |
start()
Starts the reactor.
|
void |
stop()
Stops the reactor.
|
void |
update(Selectable selectable)
Updates the specified
Selectable either emitting a
Event.Type.SELECTABLE_UPDATED event if the selectable is not terminal,
or Event.Type.SELECTABLE_FINAL if the selectable is terminal and has
not already emitted a Event.Type.SELECTABLE_FINAL event. |
void |
wakeup()
Wakes up the thread (if any) blocked in the
process() method. |
void |
yield()
Yields, causing the next call to
process() to return
successfully - without processing any events. |
long mark()
System.currentTimeMillis()
.long now()
mark()
was called.Record attachments()
Record
that can be used to associate
other objects (attachments) with this instance of the
Reactor class.void setTimeout(long timeout)
Selector.select(long)
that is called as part of process()
.timeout
- a timeout value in milliseconds, to associate with this instance of
the reactor. This can be retrieved using the
getTimeout()
methodlong getTimeout()
setTimeout(long)
or
0 if no previous value has been set.Handler getGlobalHandler()
getGlobalHandler().add(yourHandler);
void setGlobalHandler(Handler handler)
getGlobalHandler()
method.handler
- the new global handler.Handler getHandler()
getHandler().add(yourHandler);
void setHandler(Handler handler)
getHandler()
method.handler
- the new handler for this reactor.Set<ReactorChild> children()
Task
- created
using the schedule(int, Handler)
method,
Connection
- created using the
connection(Handler)
method, Acceptor
-
created using the acceptor(String, int)
method.
acceptor(String, int, Handler)
method, or
Selectable
- created using the selectable()
method.Collector collector()
Selectable selectable()
Selectable
as a child of this reactor.Selectable
.void update(Selectable selectable)
Selectable
either emitting a
Event.Type.SELECTABLE_UPDATED
event if the selectable is not terminal,
or Event.Type.SELECTABLE_FINAL
if the selectable is terminal and has
not already emitted a Event.Type.SELECTABLE_FINAL
event.selectable
- void yield()
boolean quiesced()
true
if the reactor is in quiesced state (e.g. has
no events to process). false
is returned otherwise.boolean process() throws HandlerException
true
if the reactor may have more events in the
future. For example: if there are scheduled tasks, or open
selectors. false
is returned if the reactor has
(and will have) no more events to process.HandlerException
- if an unchecked exception is thrown by one of
the handlers - it will be re-thrown attached to an instance of
HandlerException
.void wakeup()
process()
method.
This is the only method of this class that is thread safe, in that it
can be used at the same time as another thread is using the reactor.void start()
process()
.void stop() throws HandlerException
process()
.HandlerException
void run() throws HandlerException
start
, run
, and stop
method
calls.
Logically the implementation of this method is:
start(); while(process()) {} stop();
HandlerException
- if an unchecked exception is thrown by one of
the handlers - it will be re-thrown attached to an instance of
HandlerException
.Task schedule(int delay, Handler handler)
delay
- the number of milliseconds, in the future, to schedule the
task for.handler
- a handler to associate with the task. This is notified
when the deadline for the task is reached.Connection connection(Handler handler)
handler
- a handler that is notified when events occur for the
connection. Typically the host and port to connect to
would be supplied to the connection object inside the
logic which handles the Event.Type.CONNECTION_INIT
event.Acceptor acceptor(String host, int port) throws IOException
acceptor(host, port, null);
host
- port
- IOException
Acceptor acceptor(String host, int port, Handler handler) throws IOException
host
- the host name or address of the NIC to listen on.port
- the port number to listen on.handler
- if non-null
this handler is registered with
each new connection accepted by the acceptor.IOException
void free()
Copyright © 2016 The Apache Software Foundation. All rights reserved.