Class ReactorImpl
- java.lang.Object
-
- org.apache.qpid.proton.reactor.impl.ReactorImpl
-
- All Implemented Interfaces:
Extendable,Reactor
public class ReactorImpl extends java.lang.Object implements Reactor, Extendable
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.qpid.proton.reactor.Reactor
Reactor.Factory
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringCONNECTION_PEER_ADDRESS_KEYstatic ExtendableAccessor<Event,Handler>ROOT
-
Constructor Summary
Constructors Modifier Constructor Description ReactorImpl()protectedReactorImpl(IO io)protectedReactorImpl(IO io, ReactorOptions options)ReactorImpl(ReactorOptions options)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Acceptoracceptor(java.lang.String host, int port)Creates a new acceptor.Acceptoracceptor(java.lang.String host, int port, Handler handler)Creates a new acceptor.Recordattachments()java.util.Set<ReactorChild>children()Collectorcollector()Connectionconnection(Handler handler)Creates a new out-bound connection.ConnectionconnectionToHost(java.lang.String host, int port, Handler handler)Creates a new out-bound connection to the given host and port.voidfree()Frees any resources (such as sockets and selectors) held by the reactor or its children.java.lang.StringgetConnectionAddress(Connection connection)Get the address used by the connectionHandlergetGlobalHandler()HandlergetHandler()IOgetIO()ReactorOptionsgetOptions()Gets the reactor options.protected SelectorgetSelector()longgetTimeout()longmark()Updates the last time that the reactor's state has changed, potentially resulting in events being generated.longnow()booleanprocess()Process any events pending for this reactor.booleanquiesced()voidrun()Simplifies the use of the reactor by wrapping the use ofstart,run, andstopmethod calls.Taskschedule(int delay, Handler handler)Schedules execution of a task to take place at some point in the future.Selectableselectable()Creates a newSelectableas a child of this reactor.SelectableImplselectable(ReactorChild child)voidsetConnectionHost(Connection connection, java.lang.String host, int port)Set the host address used by the connectionvoidsetGlobalHandler(Handler handler)Sets a new global handler.voidsetHandler(Handler handler)Sets a new handler, that will receive any events not handled by a child of the reactor.protected voidsetSelector(Selector selector)voidsetTimeout(long timeout)The value the reactor will use forSelector.select(long)that is called as part ofReactor.process().voidstart()Starts the reactor.voidstop()Stops the reactor.voidupdate(Selectable selectable)Updates the specifiedSelectableeither emitting aEvent.Type.SELECTABLE_UPDATEDevent if the selectable is not terminal, orEvent.Type.SELECTABLE_FINALif the selectable is terminal and has not already emitted aEvent.Type.SELECTABLE_FINALevent.voidwakeup()Wakes up the thread (if any) blocked in theReactor.process()method.voidyield()Yields, causing the next call toReactor.process()to return successfully - without processing any events.
-
-
-
Field Detail
-
ROOT
public static final ExtendableAccessor<Event,Handler> ROOT
-
CONNECTION_PEER_ADDRESS_KEY
protected static final java.lang.String CONNECTION_PEER_ADDRESS_KEY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ReactorImpl
protected ReactorImpl(IO io) throws java.io.IOException
- Throws:
java.io.IOException
-
ReactorImpl
protected ReactorImpl(IO io, ReactorOptions options) throws java.io.IOException
- Throws:
java.io.IOException
-
ReactorImpl
public ReactorImpl() throws java.io.IOException- Throws:
java.io.IOException
-
ReactorImpl
public ReactorImpl(ReactorOptions options) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
mark
public long mark()
Description copied from interface:ReactorUpdates the last time that the reactor's state has changed, potentially resulting in events being generated.
-
now
public long now()
- Specified by:
nowin interfaceReactor- Returns:
- the last time that
Reactor.mark()was called.
-
free
public void free()
Description copied from interface:ReactorFrees any resources (such as sockets and selectors) held by the reactor or its children.
-
attachments
public Record attachments()
- Specified by:
attachmentsin interfaceExtendable- Specified by:
attachmentsin interfaceReactor- Returns:
- an instance of
Recordthat can be used to associate other objects (attachments) with this instance of the Reactor class.
-
getOptions
public ReactorOptions getOptions()
Description copied from interface:ReactorGets the reactor options.- Specified by:
getOptionsin interfaceReactor- Returns:
- the reactor options
-
getTimeout
public long getTimeout()
- Specified by:
getTimeoutin interfaceReactor- Returns:
- the value previously set using
Reactor.setTimeout(long)or 0 if no previous value has been set.
-
setTimeout
public void setTimeout(long timeout)
Description copied from interface:ReactorThe value the reactor will use forSelector.select(long)that is called as part ofReactor.process().- Specified by:
setTimeoutin interfaceReactor- Parameters:
timeout- a timeout value in milliseconds, to associate with this instance of the reactor. This can be retrieved using theReactor.getTimeout()method
-
getGlobalHandler
public Handler getGlobalHandler()
- Specified by:
getGlobalHandlerin interfaceReactor- Returns:
- the global handler for this reactor. Every event the reactor
sees is dispatched to the global handler. To receive every
event generated by the reactor, associate a child handler
with the global handler. For example:
getGlobalHandler().add(yourHandler);
-
setGlobalHandler
public void setGlobalHandler(Handler handler)
Description copied from interface:ReactorSets a new global handler. You probably don't want to do this and would be better adding a handler to the value returned by the {Reactor.getGlobalHandler()method.- Specified by:
setGlobalHandlerin interfaceReactor- Parameters:
handler- the new global handler.
-
getHandler
public Handler getHandler()
- Specified by:
getHandlerin interfaceReactor- Returns:
- the handler for this reactor. Every event the reactor sees,
which is not handled by a child of the reactor (such as a
timer, connection, acceptor, or selector) is passed to this
handler. To receive these events, it is recommend that you
associate a child handler with the handler returned by this
method. For example:
getHandler().add(yourHandler);
-
setHandler
public void setHandler(Handler handler)
Description copied from interface:ReactorSets a new handler, that will receive any events not handled by a child of the reactor. Note that setting a handler via this method replaces the previous handler, and will result in no further events being dispatched to the child handlers associated with the previous handler. For this reason it is recommended that you do not use this method and instead add child handlers to the value returned by theReactor.getHandler()method.- Specified by:
setHandlerin interfaceReactor- Parameters:
handler- the new handler for this reactor.
-
children
public java.util.Set<ReactorChild> children()
- Specified by:
childrenin interfaceReactor- Returns:
- a set containing the child objects associated with this reactor.
This will contain any active instances of:
Task- created using theReactor.schedule(int, Handler)method,Connection- created using theReactor.connectionToHost(String, int, Handler)method,Acceptor- created using theReactor.acceptor(String, int)method,Reactor.acceptor(String, int, Handler)method, orSelectable- created using theReactor.selectable()method.
-
collector
public Collector collector()
-
selectable
public Selectable selectable()
Description copied from interface:ReactorCreates a newSelectableas a child of this reactor.- Specified by:
selectablein interfaceReactor- Returns:
- the newly created
Selectable.
-
selectable
public SelectableImpl selectable(ReactorChild child)
-
update
public void update(Selectable selectable)
Description copied from interface:ReactorUpdates the specifiedSelectableeither emitting aEvent.Type.SELECTABLE_UPDATEDevent if the selectable is not terminal, orEvent.Type.SELECTABLE_FINALif the selectable is terminal and has not already emitted aEvent.Type.SELECTABLE_FINALevent.
-
yield
public void yield()
Description copied from interface:ReactorYields, causing the next call toReactor.process()to return successfully - without processing any events. If multiple calls can be made toyieldand only the next invocation ofReactor.process()will be affected.
-
quiesced
public boolean quiesced()
-
process
public boolean process() throws HandlerExceptionDescription copied from interface:ReactorProcess any events pending for this reactor. Events are dispatched to the handlers registered with the reactor, or child objects associated with the reactor. This method blocks until the reactor has no more work to do (and no more work pending, in terms of scheduled tasks or open selectors to process).- Specified by:
processin interfaceReactor- Returns:
trueif the reactor may have more events in the future. For example: if there are scheduled tasks, or open selectors.falseis returned if the reactor has (and will have) no more events to process.- Throws:
HandlerException- if an unchecked exception is thrown by one of the handlers - it will be re-thrown attached to an instance ofHandlerException.
-
wakeup
public void wakeup()
Description copied from interface:ReactorWakes up the thread (if any) blocked in theReactor.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.
-
start
public void start()
Description copied from interface:ReactorStarts the reactor. This method should be invoked before the first call toReactor.process().
-
stop
public void stop() throws HandlerExceptionDescription copied from interface:ReactorStops the reactor. This method should be invoked after the last call toReactor.process().- Specified by:
stopin interfaceReactor- Throws:
HandlerException
-
run
public void run() throws HandlerExceptionDescription copied from interface:ReactorSimplifies the use of the reactor by wrapping the use ofstart,run, andstopmethod calls.Logically the implementation of this method is:
start(); while(process()) {} stop();- Specified by:
runin interfaceReactor- Throws:
HandlerException- if an unchecked exception is thrown by one of the handlers - it will be re-thrown attached to an instance ofHandlerException.
-
schedule
public Task schedule(int delay, Handler handler)
Description copied from interface:ReactorSchedules execution of a task to take place at some point in the future.- Specified by:
schedulein interfaceReactor- Parameters:
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.- Returns:
- an object representing the task that has been scheduled.
-
getSelector
protected Selector getSelector()
-
setSelector
protected void setSelector(Selector selector)
-
connection
public Connection connection(Handler handler)
Description copied from interface:ReactorCreates a new out-bound connection.- Specified by:
connectionin interfaceReactor- Parameters:
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 theEvent.Type.CONNECTION_INITevent viaReactor.setConnectionHost(Connection, String, int)- Returns:
- the newly created connection object.
-
connectionToHost
public Connection connectionToHost(java.lang.String host, int port, Handler handler)
Description copied from interface:ReactorCreates a new out-bound connection to the given host and port.This method will cause Reactor to set up a network connection to the host and create a Connection for it.
- Specified by:
connectionToHostin interfaceReactor- Parameters:
host- the host to connect to (e.g. "localhost")port- the port used for the connection.handler- a handler that is notified when events occur for the connection.- Returns:
- the newly created connection object.
-
getConnectionAddress
public java.lang.String getConnectionAddress(Connection connection)
Description copied from interface:ReactorGet the address used by the connectionThis may be used to retrieve the remote peer address. Note that the returned address may be in numeric IP format.
- Specified by:
getConnectionAddressin interfaceReactor- Parameters:
connection- the Connection- Returns:
- a string containing the address in the following format:
host[:port]
-
setConnectionHost
public void setConnectionHost(Connection connection, java.lang.String host, int port)
Description copied from interface:ReactorSet the host address used by the connectionThis method will set/change the host address used by the Reactor to create an outbound network connection for the given Connection
- Specified by:
setConnectionHostin interfaceReactor- Parameters:
connection- the Connection to assign the address tohost- the address of the host to connect to (e.g. "localhost")port- the port to use for the connection.
-
acceptor
public Acceptor acceptor(java.lang.String host, int port) throws java.io.IOException
Description copied from interface:ReactorCreates a new acceptor. This is equivalent to calling:acceptor(host, port, null);
-
acceptor
public Acceptor acceptor(java.lang.String host, int port, Handler handler) throws java.io.IOException
Description copied from interface:ReactorCreates a new acceptor. This acceptor listens for in-bound connections.- Specified by:
acceptorin interfaceReactor- Parameters:
host- the host name or address of the NIC to listen on.port- the port number to listen on.handler- if non-nullthis handler is registered with each new connection accepted by the acceptor.- Returns:
- the newly created acceptor object.
- Throws:
java.io.IOException
-
getIO
public IO getIO()
-
-