Class NettyUtils


  • public final class NettyUtils
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <SuccessT,​PromiseT>
      BiConsumer<SuccessT,​? super Throwable>
      asyncPromiseNotifyingBiConsumer​(BiConsumer<SuccessT,​io.netty.util.concurrent.Promise<PromiseT>> successConsumer, io.netty.util.concurrent.Promise<PromiseT> promise)
      Creates a BiConsumer that notifies the promise of any failures either via the throwable passed into the BiConsumer or as a result of running the successConsumer.
      static String closedChannelMessage​(io.netty.channel.Channel channel)  
      static <T> io.netty.util.concurrent.GenericFutureListener<io.netty.util.concurrent.Future<T>> consumeOrPropagate​(io.netty.util.concurrent.Promise<?> destination, Consumer<T> onSuccess)
      Create a GenericFutureListener that will propagate any failures or cancellations to the provided Promise, or invoke the provided Consumer with the result of a successful operation completion.
      static Throwable decorateException​(io.netty.channel.Channel channel, Throwable originalCause)  
      static io.netty.util.concurrent.Future<?> doInEventLoop​(io.netty.util.concurrent.EventExecutor eventExecutor, Runnable runnable)
      Runs a task in the given EventExecutor.
      static void doInEventLoop​(io.netty.util.concurrent.EventExecutor eventExecutor, Runnable runnable, io.netty.util.concurrent.Promise<?> promise)
      Runs a task in the given EventExecutor.
      static <T> io.netty.util.AttributeKey<T> getOrCreateAttributeKey​(String attr)  
      static io.netty.handler.ssl.SslHandler newSslHandler​(io.netty.handler.ssl.SslContext sslContext, io.netty.buffer.ByteBufAllocator alloc, String peerHost, int peerPort, Duration handshakeTimeout)  
      static <SuccessT,​PromiseT>
      BiConsumer<SuccessT,​? super Throwable>
      promiseNotifyingBiConsumer​(Function<SuccessT,​PromiseT> successFunction, io.netty.util.concurrent.Promise<PromiseT> promise)
      Creates a BiConsumer that notifies the promise of any failures either via the Throwable passed into the BiConsumer of as a result of running the successFunction.
      static <T> io.netty.util.concurrent.GenericFutureListener<io.netty.util.concurrent.Future<T>> promiseNotifyingListener​(io.netty.util.concurrent.Promise<T> channelPromise)
      Create a GenericFutureListener that will notify the provided Promise on success and failure.
      static void runAndLogError​(NettyClientLogger log, String errorMsg, FunctionalUtils.UnsafeRunnable runnable)  
      static <T> io.netty.util.concurrent.GenericFutureListener<io.netty.util.concurrent.Future<T>> runOrPropagate​(io.netty.util.concurrent.Promise<?> destination, Runnable onSuccess)
      Create a GenericFutureListener that will propagate any failures or cancellations to the provided Promise, or invoke the provided Runnable upon successful operation completion.
      static void warnIfNotInEventLoop​(io.netty.channel.EventLoop loop)  
    • Field Detail

      • SUCCEEDED_FUTURE

        public static final io.netty.util.concurrent.SucceededFuture<?> SUCCEEDED_FUTURE
        Completed succeed future.
    • Method Detail

      • decorateException

        public static Throwable decorateException​(io.netty.channel.Channel channel,
                                                  Throwable originalCause)
      • closedChannelMessage

        public static String closedChannelMessage​(io.netty.channel.Channel channel)
      • promiseNotifyingBiConsumer

        public static <SuccessT,​PromiseT> BiConsumer<SuccessT,​? super Throwable> promiseNotifyingBiConsumer​(Function<SuccessT,​PromiseT> successFunction,
                                                                                                                        io.netty.util.concurrent.Promise<PromiseT> promise)
        Creates a BiConsumer that notifies the promise of any failures either via the Throwable passed into the BiConsumer of as a result of running the successFunction.
        Type Parameters:
        SuccessT - Success type.
        PromiseT - Type being fulfilled by the promise.
        Parameters:
        successFunction - Function called to process the successful result and map it into the result to notify the promise with.
        promise - Promise to notify of success or failure.
        Returns:
        BiConsumer that can be used in a CompletableFuture.whenComplete(BiConsumer) method.
      • asyncPromiseNotifyingBiConsumer

        public static <SuccessT,​PromiseT> BiConsumer<SuccessT,​? super Throwable> asyncPromiseNotifyingBiConsumer​(BiConsumer<SuccessT,​io.netty.util.concurrent.Promise<PromiseT>> successConsumer,
                                                                                                                             io.netty.util.concurrent.Promise<PromiseT> promise)
        Creates a BiConsumer that notifies the promise of any failures either via the throwable passed into the BiConsumer or as a result of running the successConsumer. This assumes that the successConsumer will notify the promise when it completes successfully.
        Type Parameters:
        SuccessT - Success type.
        PromiseT - Type being fulfilled by the Promise.
        Parameters:
        successConsumer - BiConsumer to call if the result is successful. Promise is also passed and must be notified on success.
        promise - Promise to notify.
        Returns:
        BiConsumer that can be used in a CompletableFuture.whenComplete(BiConsumer) method.
      • promiseNotifyingListener

        public static <T> io.netty.util.concurrent.GenericFutureListener<io.netty.util.concurrent.Future<T>> promiseNotifyingListener​(io.netty.util.concurrent.Promise<T> channelPromise)
        Create a GenericFutureListener that will notify the provided Promise on success and failure.
        Parameters:
        channelPromise - Promise to notify.
        Returns:
        GenericFutureListener
      • doInEventLoop

        public static io.netty.util.concurrent.Future<?> doInEventLoop​(io.netty.util.concurrent.EventExecutor eventExecutor,
                                                                       Runnable runnable)
        Runs a task in the given EventExecutor. Runs immediately if the current thread is in the eventExecutor.
        Parameters:
        eventExecutor - Executor to run task in.
        runnable - Task to run.
        Returns:
        The Future from from the executor.
      • doInEventLoop

        public static void doInEventLoop​(io.netty.util.concurrent.EventExecutor eventExecutor,
                                         Runnable runnable,
                                         io.netty.util.concurrent.Promise<?> promise)
        Runs a task in the given EventExecutor. Runs immediately if the current thread is in the eventExecutor. Notifies the given Promise if a failure occurs.
        Parameters:
        eventExecutor - Executor to run task in.
        runnable - Task to run.
        promise - Promise to notify if failure occurs.
      • warnIfNotInEventLoop

        public static void warnIfNotInEventLoop​(io.netty.channel.EventLoop loop)
      • getOrCreateAttributeKey

        public static <T> io.netty.util.AttributeKey<T> getOrCreateAttributeKey​(String attr)
        Returns:
        an AttributeKey for attr. This returns an existing instance if it was previously created.
      • newSslHandler

        public static io.netty.handler.ssl.SslHandler newSslHandler​(io.netty.handler.ssl.SslContext sslContext,
                                                                    io.netty.buffer.ByteBufAllocator alloc,
                                                                    String peerHost,
                                                                    int peerPort,
                                                                    Duration handshakeTimeout)
        Returns:
        a new SslHandler with ssl engine configured
      • consumeOrPropagate

        public static <T> io.netty.util.concurrent.GenericFutureListener<io.netty.util.concurrent.Future<T>> consumeOrPropagate​(io.netty.util.concurrent.Promise<?> destination,
                                                                                                                                Consumer<T> onSuccess)
        Create a GenericFutureListener that will propagate any failures or cancellations to the provided Promise, or invoke the provided Consumer with the result of a successful operation completion. This is useful for chaining together multiple futures that may depend upon each other but that may not have the same return type.

        Note that if you do not need the value returned by a successful completion (or if it returns Void) you may use runOrPropagate(Promise, Runnable) instead.

        Parameters:
        destination - the Promise to notify upon failure or cancellation
        onSuccess - the Consumer to invoke upon success
      • runOrPropagate

        public static <T> io.netty.util.concurrent.GenericFutureListener<io.netty.util.concurrent.Future<T>> runOrPropagate​(io.netty.util.concurrent.Promise<?> destination,
                                                                                                                            Runnable onSuccess)
        Create a GenericFutureListener that will propagate any failures or cancellations to the provided Promise, or invoke the provided Runnable upon successful operation completion. This is useful for chaining together multiple futures that may depend upon each other but that may not have the same return type.
        Parameters:
        destination - the Promise to notify upon failure or cancellation
        onSuccess - the Runnable to invoke upon success