public class DefaultClientThreadingStrategy extends Object implements ClientThreadingStrategy
ExecutorService
for delivering notifications.Constructor and Description |
---|
DefaultClientThreadingStrategy(ErrorHandler errorHandler)
Creates a default threading strategy that uses the specified error
handler.
|
DefaultClientThreadingStrategy(ErrorHandler errorHandler,
ExecutorService notificationExecutorService,
boolean shutdownNotificationExecutorServiceOnDestruction)
Creates a default threading strategy that uses the specified error
handler and executor service.
|
Modifier and Type | Method and Description |
---|---|
void |
addServerConnection(InetSocketAddress serverAddress,
CommunicationProcessor communicationProcessor)
Registers a communication processor that handles a connection with a
specific server.
|
void |
destroy()
Destroys the threading strategy.
|
void |
notifyConnectionListener(ChannelAccessChannel channel,
ChannelAccessConnectionListener listener,
boolean nowConnected)
Submits a notification request for a connection listener.
|
<T> void |
notifyFutureCompletionListener(ListenableFuture<T> future,
FutureCompletionListener<? super T> listener)
Submits a notification request for a future completion listener.
|
<T extends ChannelAccessGettableValue<?>> |
notifyMonitorListenerError(ChannelAccessMonitor<? extends T> monitor,
ChannelAccessMonitorListener<? super T> listener,
ChannelAccessStatus status,
String message)
Submits an error notification request for a monitor listener.
|
<T extends ChannelAccessGettableValue<?>> |
notifyMonitorListenerEvent(ChannelAccessMonitor<? extends T> monitor,
ChannelAccessMonitorListener<? super T> listener,
T value)
Submits an event notification request for a monitor listener.
|
void |
removeServerConnection(InetSocketAddress serverAddress,
CommunicationProcessor communicationProcessor)
Unregisters a communication processor that handles a connection with a
specific server.
|
void |
setBeaconDetector(CommunicationProcessor communicationProcessor)
Sets the communication processor for the
BeaconDetector . |
void |
setChannelNameResolver(CommunicationProcessor communicationProcessor)
Sets the communication processor used for the
ChannelNameResolver
. |
public DefaultClientThreadingStrategy(ErrorHandler errorHandler)
Creates a default threading strategy that uses the specified error handler.
The error handler is used when one of the threads that is managed by this strategy encounters an uncaught exception.
Using this constructor is equivalent to using
DefaultClientThreadingStrategy(errorHandler, null, false)
.
errorHandler
- error handler to be used by this threading strategy. This
error handler is used if an exception is thrown by one of the
communication processors that are registered with this
strategy. Typically, processors should handle exceptions
themselves, so this is only a fall-back solution.public DefaultClientThreadingStrategy(ErrorHandler errorHandler, ExecutorService notificationExecutorService, boolean shutdownNotificationExecutorServiceOnDestruction)
Creates a default threading strategy that uses the specified error handler and executor service.
The error handler is used when one of the threads that is managed by this strategy encounters an uncaught exception.
If the supplied notificationExecutorService
is not
null
, it is used when one of this strategy's listener
notification methods is called. Instead of calling the corresponding
listener method directly, a task is scheduled with the executor service
in order to call the listener method in a separate thread at a later
time. This class ensures that notifications are still delivered in the
right order, regardless of how the executor service schedules the tasks
submitted to it. If the notificationExecutorService
is
null
, the listener's methods are called right in the thread
calling this strategy's notification method.
For performance reasons, a notificationExecutorService
should only be used if it is really necessary. In general, notifying
listeners right in the calling thread is the best choice. Using separate
threads should only be considered if listeners might act erratically (by
blocking or running for an extended amount of time) and fixing the
listeners is not feasible. Due to internal optimizations, setting the
notificationExecutorService
to null
is much
more efficient than providing an executor service that runs its submitted
tasks in the calling thread.
If shutdownNotificationExecutorServiceOnDestruction
is
true
and notificationExecutorService
is not
null
, the notificationExecutorService
's
shutdown()
method is called when this strategy's
destroy()
method is called. Otherwise, the executor service
is not shutdown when this threading strategy is destroyed. The latter
behavior can be useful if the executor service that is passed to this
constructor is shared with another component that should stay operative
when this threading strategy is destroyed.
errorHandler
- error handler to be used by this threading strategy. This
error handler is used if an exception is thrown by one of the
communication processors that are registered with this
strategy. Typically, processors should handle exceptions
themselves, so this is only a fall-back solution. The error
handler is also used when the
notificationExecutorService
is not
null
and one of the listener methods invoked
through this execution service throws an exception.notificationExecutorService
- executor service used to run listener notifications. If
null
, listeners are notified right in the calling
thread.shutdownNotificationExecutorServiceOnDestruction
- if true
, the
notificationExecutorService
is shutdown when this
strategy is destroyed. If false
, the
notificationExecutorService
is kept alive past
the destruction of this strategy.public void addServerConnection(InetSocketAddress serverAddress, CommunicationProcessor communicationProcessor)
ClientThreadingStrategy
addServerConnection
in interface ClientThreadingStrategy
serverAddress
- IP address and port of the server. A threading strategy might
use this information to decide which thread should be used.communicationProcessor
- communication processor handling the connection with the
specified server.public void removeServerConnection(InetSocketAddress serverAddress, CommunicationProcessor communicationProcessor)
ClientThreadingStrategy
removeServerConnection
in interface ClientThreadingStrategy
serverAddress
- IP address and port of the server. A threading strategy might
use this information to decide which thread is used by the
communication processor.communicationProcessor
- communication processor handling the connection with the
specified server.public void notifyConnectionListener(ChannelAccessChannel channel, ChannelAccessConnectionListener listener, boolean nowConnected)
ClientThreadingStrategy
Submits a notification request for a connection listener.
An implementation may choose to notify the listener in the calling thread
(before returning) or it may choose to queue the notification request for
processing by a different thread. In any case, each call of this method
must result in exactly one corresponding call of
listener.connectionStateChanged(channel, nowConnected)
.
When an implementation chooses to notify the listener asynchronously, it
still has to ensure that the listener receives notifications in the
correct order. This means that for every call of this method using the
same channel
-listener
pair, the calls of
listener.connectionStateChanged(...)
must be made in the
same order in which this method was called. Calls specifying different
listeners or channels may be reordered arbitrarily.
For deciding whether a channel is the same channel as specified in another request and whether a listener is the same listener as specified in another request, object identity should be used instead of object equality. used.
notifyConnectionListener
in interface ClientThreadingStrategy
channel
- channel that is the source of the event.listener
- listener that shall be notified.nowConnected
- new connection state of which the listener shall be notified.public <T extends ChannelAccessGettableValue<?>> void notifyMonitorListenerError(ChannelAccessMonitor<? extends T> monitor, ChannelAccessMonitorListener<? super T> listener, ChannelAccessStatus status, String message)
ClientThreadingStrategy
Submits an error notification request for a monitor listener.
An implementation may choose to notify the listener in the calling thread
(before returning) or it may choose to queue the notification request for
processing by a different thread. In any case, each call of this method
must result in exactly one corresponding call of
listener.monitorError(monitor, status, message)
.
When an implementation chooses to notify the listener asynchronously, it
still has to ensure that the listener receives notifications in the
correct order. This means that for every call of this method using the
same monitor
-listener
pair, the calls of
listener.monitorError(...)
must be made in the same order in
which this method was called. Calls specifying different listeners or
monitors may be reordered arbitrarily.
For deciding whether a monitor is the same monitor as specified in another request and whether a listener is the same listener as specified in another request, object identity should be used instead of object equality. used.
notifyMonitorListenerError
in interface ClientThreadingStrategy
T
- value type of the monitor and listener.monitor
- monitor that is the source of the event.listener
- listener that shall be notified.status
- status code that is associated with the event.message
- message that is associated with the event (may be
null
).public <T extends ChannelAccessGettableValue<?>> void notifyMonitorListenerEvent(ChannelAccessMonitor<? extends T> monitor, ChannelAccessMonitorListener<? super T> listener, T value)
ClientThreadingStrategy
Submits an event notification request for a monitor listener.
An implementation may choose to notify the listener in the calling thread
(before returning) or it may choose to queue the notification request for
processing by a different thread. In any case, each call of this method
must result in exactly one corresponding call of
listener.monitorEvent(monitor, value)
.
When an implementation chooses to notify the listener asynchronously, it
still has to ensure that the listener receives notifications in the
correct order. This means that for every call of this method using the
same monitor
-listener
pair, the calls of
listener.monitorValue(...)
must be made in the same order in
which this method was called. Calls specifying different listeners or
monitors may be reordered arbitrarily.
For deciding whether a monitor is the same monitor as specified in another request and whether a listener is the same listener as specified in another request, object identity should be used instead of object equality. used.
notifyMonitorListenerEvent
in interface ClientThreadingStrategy
T
- value type of the monitor and listener. This the type of value
which is passed as the event value.monitor
- monitor that is the source of the event.listener
- listener that shall be notified.value
- event value. Typically, this is a value update for the
monitor's channel.public <T> void notifyFutureCompletionListener(ListenableFuture<T> future, FutureCompletionListener<? super T> listener)
ClientThreadingStrategy
Submits a notification request for a future completion listener.
An implementation may choose to notify the listener in the calling thread
(before returning) or it may choose to queue the notification request for
processing by a different thread. In any case, each call of this method
must result in exactly one corresponding call of
listener.completed(future)
.
An implementation may choose to reorder notification requests arbitrarily.
notifyFutureCompletionListener
in interface ClientThreadingStrategy
T
- type of the future's value.future
- future that completed and is the source of the event.listener
- listener that shall be notified.public void setBeaconDetector(CommunicationProcessor communicationProcessor)
ClientThreadingStrategy
BeaconDetector
. As there
is only one beacon detector for each client, the threading strategy does
not have to maintain a list, but can simply assume that this method is
only called once.setBeaconDetector
in interface ClientThreadingStrategy
communicationProcessor
- communication processor used by the beacon detector.public void setChannelNameResolver(CommunicationProcessor communicationProcessor)
ClientThreadingStrategy
ChannelNameResolver
. As there is only one channel name resolver for each client, the
threading strategy does not have to maintain a list, but can simply
assume that this method is only called once.setChannelNameResolver
in interface ClientThreadingStrategy
communicationProcessor
- communication processor used by the channel name resolver.public void destroy()
ClientThreadingStrategy
destroy
in interface ClientThreadingStrategy
Copyright © 2014–2017 aquenos GmbH. All rights reserved.