ServerRunner.
An instance of this object is created with a ServerListener and an
Executor. On invocation of the
start() method, it passes itself to the given 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 LoggingContext and is then passed to the 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.
| Modifier and Type | Class and Description |
|---|---|
| private class | ConcurrentServerRunner.ClientWrapper
A wrapper for a |
| Modifier and Type | Field and Description |
|---|---|
| private final Collection | |
| private final Lock | |
| private final Executor | |
| private final ServerListener | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | ConcurrentServerRunner(ServerListener<T>
the listener from which the server will accept new clients listener, Executor an executor that will facilitate execution of the listening
and client-handling tasks; while any executor)Executor is
allowed here, outside of unit testing the only reasonable
choice is a bounded thread pool of some kind.Constructs a new server runner. |
| Modifier and Type | Method and Description |
|---|---|
| public void | accept(ClientVisitor<T>
the subject visitor visitor)Implements ch. |
| private void | addClient(T
the client to add client)Adds a client to the collection of those being tracked by the server. |
| protected abstract boolean | Returns: true if configuration was successful; if the return value is
false the client connection will be droppedthe subject client client)Configures a connected client. |
| private Collection | Returns: collection of client objectsCreates a copy of the collection of all clients that are presently being tracked by the server. |
| public boolean | isRunning()
Implements ch. |
| private void | removeClient(T
the client to remote client)Removes a client from the collection of those being tracked by the server. |
| public void | |
| protected void | |
| public void |
| clients | back to summary |
|---|---|
| private final Collection<T> clients | |
| clientsLock | back to summary |
|---|---|
| private final Lock clientsLock | |
| executor | back to summary |
|---|---|
| private final Executor executor | |
| listener | back to summary |
|---|---|
| private final ServerListener<T> listener | |
| running | back to summary |
|---|---|
| private boolean running | |
| ConcurrentServerRunner | back to summary |
|---|---|
| public ConcurrentServerRunner(ServerListener<T> listener, Executor executor) Constructs a new server runner.
| |
| accept | back to summary |
|---|---|
| public void accept(ClientVisitor<T> visitor) Implements ch. Doc from ch. Presents each connected client to the given visitor.
| |
| addClient | back to summary |
|---|---|
| private void addClient(T client) Adds a client to the collection of those being tracked by the server.
| |
| configureClient | back to summary |
|---|---|
| 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
| |
| copyClients | back to summary |
|---|---|
| private Collection Creates a copy of the collection of all clients that are presently being tracked by the server.
| |
| isRunning | back to summary |
|---|---|
| public boolean isRunning() Implements ch. Doc from ch. Gets a flag indicating whether the server is currently running.
| |
| removeClient | back to summary |
|---|---|
| private void removeClient(T client) Removes a client from the collection of those being tracked by the server.
| |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation. | |
| setRunning | back to summary |
|---|---|
| protected void setRunning(boolean running) | |
| stop | back to summary |
|---|---|
| public void stop() throws IOException Implements ch. Doc from ch. 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. | |
Client responsible for ensuring that client tracking
is performed properly.
| Modifier and Type | Field and Description |
|---|---|
| private final T |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements ch. |
| public void |
| delegate | back to summary |
|---|---|
| private final T delegate | |
| ClientWrapper | back to summary |
|---|---|
| public ClientWrapper(T client) | |
| close | back to summary |
|---|---|
| public void close() Implements ch. Doc from ch. Closes any resources that are held by the client.
Note that (as described in Doug Lea's discussion about interrupting I/O
operations in "Concurrent Programming in Java" - Addison-Wesley Professional,
2nd edition, 1999) this method is used to interrupt any blocked I/O operation
in the client when the server is shutting down. The client implementation
must anticipate this potential, and gracefully exit when the blocked I/O
operation throws the relevant
Note also, that unlike | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation. | |