Class ConcurrentServerRunner<T extends Client>
- java.lang.Object
-
- ch.qos.logback.core.spi.ContextAwareBase
-
- ch.qos.logback.core.net.server.ConcurrentServerRunner<T>
-
- All Implemented Interfaces:
ServerRunner<T>
,ContextAware
,java.lang.Runnable
- Direct Known Subclasses:
RemoteAppenderServerRunner
,RemoteReceiverServerRunner
public abstract class ConcurrentServerRunner<T extends Client> extends ContextAwareBase implements java.lang.Runnable, ServerRunner<T>
A concurrentServerRunner
.An instance of this object is created with a
ServerListener
and anExecutor
. On invocation of the#start()
method, it passes itself to the givenExecutor
and returns immediately. On invocation of itsrun()
method by theExecutor
it begins accepting client connections via itsServerListener
. As each newClient
is accepted, the client is configured with the runner'sLoggingContext
and is then passed to theExecutor
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 invokingClient.close()
effectively interrupting any blocked I/O calls and causing these concurrent subtasks to exit gracefully). This ensures that before thestop()
method returns (1) all I/O resources have been released and (2) all of the threads of theExecutor
are idle.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ConcurrentServerRunner.ClientWrapper
A wrapper for aClient
responsible for ensuring that client tracking is performed properly.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<T>
clients
private java.util.concurrent.locks.Lock
clientsLock
private java.util.concurrent.Executor
executor
private ServerListener<T>
listener
private boolean
running
-
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
-
-
Constructor Summary
Constructors Constructor Description ConcurrentServerRunner(ServerListener<T> listener, java.util.concurrent.Executor executor)
Constructs a new server runner.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
accept(ClientVisitor<T> visitor)
Presents each connected client to the given visitor.private void
addClient(T client)
Adds a client to the collection of those being tracked by the server.protected abstract boolean
configureClient(T client)
Configures a connected client.private java.util.Collection<T>
copyClients()
Creates a copy of the collection of all clients that are presently being tracked by the server.boolean
isRunning()
Gets a flag indicating whether the server is currently running.private void
removeClient(T client)
Removes a client from the collection of those being tracked by the server.void
run()
protected void
setRunning(boolean running)
void
stop()
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, setContext
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
-
-
-
Field Detail
-
clientsLock
private final java.util.concurrent.locks.Lock clientsLock
-
listener
private final ServerListener<T extends Client> listener
-
executor
private final java.util.concurrent.Executor executor
-
running
private boolean running
-
-
Constructor Detail
-
ConcurrentServerRunner
public ConcurrentServerRunner(ServerListener<T> listener, java.util.concurrent.Executor executor)
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 anyExecutor
is allowed here, outside of unit testing the only reasonable choice is a bounded thread pool of some kind.
-
-
Method Detail
-
isRunning
public boolean isRunning()
Gets a flag indicating whether the server is currently running.- Specified by:
isRunning
in interfaceServerRunner<T extends Client>
- Returns:
- flag state
-
setRunning
protected void setRunning(boolean running)
-
stop
public void stop() throws java.io.IOException
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:
stop
in interfaceServerRunner<T extends Client>
- Throws:
java.io.IOException
-
accept
public void accept(ClientVisitor<T> visitor)
Presents each connected client to the given visitor.- Specified by:
accept
in interfaceServerRunner<T extends Client>
- Parameters:
visitor
- the subject visitor
-
copyClients
private java.util.Collection<T> 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()
- Specified by:
run
in interfacejava.lang.Runnable
-
configureClient
protected abstract boolean configureClient(T client)
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:
true
if configuration was successful; if the return value isfalse
the client connection will be dropped
-
addClient
private void addClient(T client)
Adds a client to the collection of those being tracked by the server.- Parameters:
client
- the client to add
-
removeClient
private void removeClient(T client)
Removes a client from the collection of those being tracked by the server.- Parameters:
client
- the client to remote
-
-