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 enumEnumeration of events for which to listen. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidClears all events until a subsequent call tosetEvents(Event...)is made.onClose(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscriptionfor when the underlying connection was closed.onError(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscriptionfor when a socket has encountered an error.onRead(Consumer<AsyncConnection<ContextT, SocketT>> asyncConnectionConsumer) Registers aSubscriptionfor 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 aSubscriptionfor when a socket is ready to write data.voidrecycle()Returns thisAsyncConnection.voidsetEvents(AsyncConnection.Event... events) Ensures that theAsyncConnectionis registered for the supplied events.voidsignal(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 theAsyncConnectionfrom 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 underlyingAsyncConnectionServicethat 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 theAsyncConnectionis registered for the supplied events.- Parameters:
events- one of manyAsyncConnection.Events
-
onRead
Registers aSubscriptionfor when a socket has read data.- Parameters:
asyncConnectionConsumer- theConsumer<AsyncConnection<ContextT,to receive the eventSocketT>> - Returns:
Subscription
-
onWrite
Registers aSubscriptionfor when a socket is ready to write data.- Parameters:
asyncConnectionConsumer- theConsumer<AsyncConnection<ContextT,to receive the eventSocketT>> - Returns:
Subscription
-
onError
Registers aSubscriptionfor when a socket has encountered an error.- Parameters:
asyncConnectionConsumer- theConsumer<AsyncConnection<ContextT,to receive the eventSocketT>> - Returns:
Subscription
-
onClose
Registers aSubscriptionfor when the underlying connection was closed.- Parameters:
asyncConnectionConsumer- theConsumerto receive the event- Returns:
Subscription
-
onRecycle
Subscribes to an event indicating that the connection was recycled.- Parameters:
pooledAsyncConnectionConsumer- theConsumerto receive the event.- Returns:
- a
Subscriptionto the event
-