Top Description Inners Fields Constructors Methods
ch.qos.logback.core.net.server

public abstract Class ConcurrentServerRunner<T extends ch.qos.logback.core.net.server.Client>

extends ContextAwareBase
implements Runnable, ServerRunner<T extends Client>
Class Inheritance
All Implemented Interfaces
ch.qos.logback.core.net.server.ServerRunner, java.lang.Runnable, ch.qos.logback.core.spi.ContextAware
Known Direct Subclasses
ch.qos.logback.core.net.server.RemoteReceiverServerRunner
Imports
java.io.IOException, java.util.ArrayList, .Collection, java.util.concurrent.Executor, .RejectedExecutionException, java.util.concurrent.locks.Lock, .ReentrantLock, ch.qos.logback.core.spi.ContextAwareBase

A concurrent 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.

Author
Carl Harris

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
ConcurrentServerRunner.ClientWrapper

A wrapper for a Client responsible for ensuring that client tracking is performed properly.

Field Summary

Modifier and TypeField and Description
private final Collection<T>
private final Lock
private final Executor
private final ServerListener<T>
private boolean
Inherited from ch.qos.logback.core.spi.ContextAwareBase:
context

Constructor Summary

AccessConstructor 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 is allowed here, outside of unit testing the only reasonable choice is a bounded thread pool of some kind.
executor
)

Constructs a new server runner.

Method Summary

Modifier and TypeMethod and Description
public void
accept(ClientVisitor<T>
the subject visitor
visitor
)

Implements ch.qos.logback.core.net.server.ServerRunner.accept.

Presents each connected client to the given visitor.
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 dropped
configureClient
(T
the subject client
client
)

Configures a connected client.

private Collection<T>

Returns:

collection of client objects
copyClients
()

Creates a copy of the collection of all clients that are presently being tracked by the server.

public boolean
isRunning()

Implements ch.qos.logback.core.net.server.ServerRunner.isRunning.

Gets a flag indicating whether the server is currently running.
private void
removeClient(T
the client to remote
client
)

Removes a client from the collection of those being tracked by the server.

public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
protected void
setRunning(boolean running)

public void
stop()

Implements ch.qos.logback.core.net.server.ServerRunner.stop.

Stops execution of the runner.
Inherited from ch.qos.logback.core.spi.ContextAwareBase:
addErroraddErroraddInfoaddInfoaddStatusaddWarnaddWarngetContextgetDeclaredOrigingetStatusManagersetContext

Field Detail

clientsback to summary
private final Collection<T> clients
clientsLockback to summary
private final Lock clientsLock
executorback to summary
private final Executor executor
listenerback to summary
private final ServerListener<T> listener
runningback to summary
private boolean running

Constructor Detail

ConcurrentServerRunnerback to summary
public ConcurrentServerRunner(ServerListener<T> listener, Executor executor)

Constructs a new server runner.

Parameters
listener:ServerListener<T>

the listener from which the server will accept new clients

executor:Executor

an executor that will facilitate execution of the listening and client-handling tasks; while any Executor is allowed here, outside of unit testing the only reasonable choice is a bounded thread pool of some kind.

Method Detail

acceptback to summary
public void accept(ClientVisitor<T> visitor)

Implements ch.qos.logback.core.net.server.ServerRunner.accept.

Doc from ch.qos.logback.core.net.server.ServerRunner.accept.

Presents each connected client to the given visitor.

Parameters
visitor:ClientVisitor<T>

the subject visitor

addClientback to summary
private void addClient(T client)

Adds a client to the collection of those being tracked by the server.

Parameters
client:T

the client to add

configureClientback 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 Client#run() method is invoked.

Parameters
client:T

the subject client

Returns:boolean

true if configuration was successful; if the return value is false the client connection will be dropped

copyClientsback to summary
private Collection<T> copyClients()

Creates a copy of the collection of all clients that are presently being tracked by the server.

Returns:Collection<T>

collection of client objects

isRunningback to summary
public boolean isRunning()

Implements ch.qos.logback.core.net.server.ServerRunner.isRunning.

Doc from ch.qos.logback.core.net.server.ServerRunner.isRunning.

Gets a flag indicating whether the server is currently running.

Returns:boolean

flag state

removeClientback to summary
private void removeClient(T client)

Removes a client from the collection of those being tracked by the server.

Parameters
client:T

the client to remote

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

setRunningback to summary
protected void setRunning(boolean running)
stopback to summary
public void stop() throws IOException

Implements ch.qos.logback.core.net.server.ServerRunner.stop.

Doc from ch.qos.logback.core.net.server.ServerRunner.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.

ch.qos.logback.core.net.server back to summary

private Class ConcurrentServerRunner.ClientWrapper

extends Object
implements Client
Class Inheritance
  • java.lang.Object
  • ch.qos.logback.core.net.server.ConcurrentServerRunner.ClientWrapper
All Implemented Interfaces
ch.qos.logback.core.net.server.Client, java.io.Closeable, java.lang.AutoCloseable, java.lang.Runnable

A wrapper for a Client responsible for ensuring that client tracking is performed properly.

Field Summary

Modifier and TypeField and Description
private final T

Constructor Summary

AccessConstructor and Description
public
ClientWrapper(T client)

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements ch.qos.logback.core.net.server.Client.close.

Closes any resources that are held by the client.
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateback to summary
private final T delegate

Constructor Detail

ClientWrapperback to summary
public ClientWrapper(T client)

Method Detail

closeback to summary
public void close()

Implements ch.qos.logback.core.net.server.Client.close.

Doc from ch.qos.logback.core.net.server.Client.close.

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 IOException subclass.

Note also, that unlike Closeable#close() this method is not permitted to propagate any IOException that occurs when closing the underlying resource(s).

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.