Package dev.getelements.elements.rt
Interface AsyncConnection<ContextT,SocketT>
- All Superinterfaces:
AutoCloseable
,Connection<ContextT,
SocketT>
Represents an asynchornous type of
Connection
. Unless otherwise indicated, instances of
AsyncConnection
aren't thread-safe. Therefore you must ensure that all manipulation of
the AsyncConnection
instance is performed within a callback from the subscribed events.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enum
Enumeration of events for which to listen. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Clears all events until a subsequent call tosetEvents(Event...)
is made.onClose
(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscription
for when the underlying connection was closed.onError
(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscription
for when a socket has encountered an error.onRead
(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscription
for when a socket has read data.onRecycle
(Consumer<AsyncConnection<ContextT, SocketT>> pooledAsyncConnectionConsumer) Subscribes to an event indicating that the connection was recycled.onWrite
(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscription
for when a socket is ready to write data.void
recycle()
Returns thisAsyncConnection
.void
setEvents
(AsyncConnection.Event... events) Ensures that theAsyncConnection
is registered for the supplied events.void
signal
(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Sends a signal to thisAsyncConnection
.default <T> CompletionStage
<T> signalAndComputeCompletion
(Function<AsyncConnection<ContextT, SocketT>, T> asyncConnectionConsumer) Computes a value on the connection's IO Thread.default CompletionStage
<Void> signalAndComputeCompletionV
(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Computes a value on the connection's IO Thread.Methods inherited from interface dev.getelements.elements.rt.Connection
close, context, socket
-
Method Details
-
signal
Sends a signal to thisAsyncConnection
. The signal will run on theAsyncConnection
's thread and can be used to safely manipulate theAsyncConnection
from an outside thread. This method, and other "signal" methods, may be invoked from any thread.- Parameters:
asyncConnectionConsumer
- theConsumer<AsyncConnection>
to receive the event
-
signalAndComputeCompletion
default <T> CompletionStage<T> signalAndComputeCompletion(Function<AsyncConnection<ContextT, SocketT>, T> asyncConnectionConsumer) Computes a value on the connection's IO Thread. This will return aCompletionStage<T>
which may be used to retrieve the value.- Type Parameters:
T
- the type to compute- Parameters:
asyncConnectionConsumer
- the consumer.- Returns:
- the
CompletionStage<T>
-
signalAndComputeCompletionV
default CompletionStage<Void> signalAndComputeCompletionV(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Computes a value on the connection's IO Thread. This will return aCompletionStage<Void>
which may be used to block until the IO thread completes the supplied task.- Parameters:
asyncConnectionConsumer
- the consumer to execute the task- Returns:
- the
CompletionStage<Void>
-
recycle
void recycle()Returns thisAsyncConnection
. This hints to the underlyingAsyncConnectionService
that the client code is done using the connection. Depending on circumstances, this may simply close the connection. -
clearEvents
default void clearEvents()Clears all events until a subsequent call tosetEvents(Event...)
is made. This may be a shortcut for simply passing no arguments tosetEvents(Event...)
. -
setEvents
Ensures that theAsyncConnection
is registered for the supplied events.- Parameters:
events
- one of manyAsyncConnection.Event
s
-
onRead
Registers aSubscription
for when a socket has read data.- Parameters:
asyncConnectionConsumer
- theConsumer<AsyncConnection<ContextT,
to receive the eventSocketT>> - Returns:
Subscription
-
onWrite
Registers aSubscription
for when a socket is ready to write data.- Parameters:
asyncConnectionConsumer
- theConsumer<AsyncConnection<ContextT,
to receive the eventSocketT>> - Returns:
Subscription
-
onError
Registers aSubscription
for when a socket has encountered an error.- Parameters:
asyncConnectionConsumer
- theConsumer<AsyncConnection<ContextT,
to receive the eventSocketT>> - Returns:
Subscription
-
onClose
Registers aSubscription
for when the underlying connection was closed.- Parameters:
asyncConnectionConsumer
- theConsumer
to receive the event- Returns:
Subscription
-
onRecycle
Subscribes to an event indicating that the connection was recycled.- Parameters:
pooledAsyncConnectionConsumer
- theConsumer
to receive the event.- Returns:
- a
Subscription
to the event
-