Class ConcurrentServerRunner<T extends Client>
- All Implemented Interfaces:
ServerRunner<T>,ContextAware,Runnable
- Direct Known Subclasses:
RemoteAppenderServerRunner,RemoteReceiverServerRunner
ServerRunner.
An instance of this object is created with a ServerListener and
an Executor. On invocation of the
invalid reference
#start()Executor and returns immediately. On
invocation of its run() method by the Executor it begins
accepting client connections via its ServerListener. As each
new Client is accepted, the client is configured with the
runner's
invalid reference
LoggingContext
Executor for concurrent execution of the client's service loop.
On invocation of the stop() method, the runner closes the listener
and each of the connected clients (by invoking Client.close()
effectively interrupting any blocked I/O calls and causing these concurrent
subtasks to exit gracefully). This ensures that before the stop()
method returns (1) all I/O resources have been released and (2) all
of the threads of the Executor are idle.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classA wrapper for aClientresponsible for ensuring that client tracking is performed properly. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Collection<T> private final Lockprivate final Executorprivate final ServerListener<T> private booleanFields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentServerRunner(ServerListener<T> listener, Executor executor) Constructs a new server runner. -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(ClientVisitor<T> visitor) Presents each connected client to the given visitor.private voidAdds a client to the collection of those being tracked by the server.protected abstract booleanconfigureClient(T client) Configures a connected client.private Collection<T> Creates a copy of the collection of all clients that are presently being tracked by the server.booleanGets a flag indicating whether the server is currently running.private voidremoveClient(T client) Removes a client from the collection of those being tracked by the server.voidrun()protected voidsetRunning(boolean running) voidstop()Stops execution of the runner.Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContextMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
Field Details
-
clientsLock
-
clients
-
listener
-
executor
-
running
private boolean running
-
-
Constructor Details
-
ConcurrentServerRunner
Constructs a new server runner.- Parameters:
listener- the listener from which the server will accept new clientsexecutor- a executor that will facilitate execution of the listening and client-handling tasks; while anyExecutoris allowed here, outside of unit testing the only reasonable choice is a bounded thread pool of some kind.
-
-
Method Details
-
isRunning
public boolean isRunning()Gets a flag indicating whether the server is currently running.- Specified by:
isRunningin interfaceServerRunner<T extends Client>- Returns:
- flag state
-
setRunning
protected void setRunning(boolean running) -
stop
Stops execution of the runner.This method must cause all I/O and thread resources associated with the runner to be released. If the receiver has not been started, this method must have no effect.
- Specified by:
stopin interfaceServerRunner<T extends Client>- Throws:
IOException
-
accept
Presents each connected client to the given visitor.- Specified by:
acceptin interfaceServerRunner<T extends Client>- Parameters:
visitor- the subject visitor
-
copyClients
Creates a copy of the collection of all clients that are presently being tracked by the server.- Returns:
- collection of client objects
-
run
public void run() -
configureClient
Configures a connected client.A subclass implements this method to perform any necessary configuration of the client object before its
Runnable.run()method is invoked.- Parameters:
client- the subject client- Returns:
trueif configuration was successful; if the return value isfalsethe client connection will be dropped
-
addClient
Adds a client to the collection of those being tracked by the server.- Parameters:
client- the client to add
-
removeClient
Removes a client from the collection of those being tracked by the server.- Parameters:
client- the client to remote
-