Java Code Examples for io.netty.util.concurrent.Promise#trySuccess()

The following examples show how to use io.netty.util.concurrent.Promise#trySuccess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DnsQueryContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void setSuccess(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) {
    parent.queryContextManager.remove(nameServerAddr(), id);

    // Cancel the timeout task.
    final ScheduledFuture<?> timeoutFuture = this.timeoutFuture;
    if (timeoutFuture != null) {
        timeoutFuture.cancel(false);
    }

    Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise = this.promise;
    if (promise.setUncancellable()) {
        @SuppressWarnings("unchecked")
        AddressedEnvelope<DnsResponse, InetSocketAddress> castResponse =
                (AddressedEnvelope<DnsResponse, InetSocketAddress>) envelope.retain();
        if (!promise.trySuccess(castResponse)) {
            // We failed to notify the promise as it was failed before, thus we need to release the envelope
            envelope.release();
        }
    }
}
 
Example 2
Source File: DnsNameResolver.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
boolean finishResolve(
    Class<? extends InetAddress> addressType, List<DnsCacheEntry> resolvedEntries,
    Promise<List<InetAddress>> promise) {

    List<InetAddress> result = null;
    final int numEntries = resolvedEntries.size();
    for (int i = 0; i < numEntries; i++) {
        final InetAddress a = resolvedEntries.get(i).address();
        if (addressType.isInstance(a)) {
            if (result == null) {
                result = new ArrayList<InetAddress>(numEntries);
            }
            result.add(a);
        }
    }

    if (result != null) {
        promise.trySuccess(result);
        return true;
    }
    return false;
}
 
Example 3
Source File: InflightNameResolver.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static <T> void transferResult(Future<T> src, Promise<T> dst) {
    if (src.isSuccess()) {
        dst.trySuccess(src.getNow());
    } else {
        dst.tryFailure(src.cause());
    }
}
 
Example 4
Source File: SimpleChannelPool.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void notifyConnect(ChannelFuture future, Promise<Channel> promise) {
        if (future.isSuccess()) {
            Channel channel = future.channel();
            if (!promise.trySuccess(channel)) {
                // Promise was completed in the meantime (like cancelled), just release the channel again承诺完成的同时(如取消),只是释放渠道再次
//                如果没有连接成功释放channel
                release(channel);
            }
        } else {
            promise.tryFailure(future.cause());
        }
    }
 
Example 5
Source File: PromiseNotificationUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Try to mark the {@link Promise} as success and log if {@code logger} is not {@code null} in case this fails.尝试将承诺标记为成功,如果日志记录器在失败时不为空,则记录日志。
 */
public static <V> void trySuccess(Promise<? super V> p, V result, InternalLogger logger) {
    if (!p.trySuccess(result) && logger != null) {
        Throwable err = p.cause();
        if (err == null) {
            logger.warn("Failed to mark a promise as success because it has succeeded already: {}", p);
        } else {
            logger.warn(
                    "Failed to mark a promise as success because it has failed already: {}, unnotified cause:",
                    p, err);
        }
    }
}
 
Example 6
Source File: ConnectionFactory.java    From drift with Apache License 2.0 5 votes vote down vote up
private static void notifyConnect(ChannelFuture future, Promise<Channel> promise)
{
    if (future.isSuccess()) {
        Channel channel = future.channel();
        if (!promise.trySuccess(channel)) {
            // Promise was completed in the meantime (likely cancelled), just release the channel again
            channel.close();
        }
    }
    else {
        promise.tryFailure(future.cause());
    }
}
 
Example 7
Source File: HealthCheckedChannelPool.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the channel returned by the underlying channel pool is healthy. If so, complete the result future with the
 * channel returned by the underlying pool. If not, close the channel and try to get a different one.
 *
 * @param delegateFuture A completed promise as a result of invoking delegate.acquire().
 * @param resultFuture The future that should be completed with the healthy, acquired channel.
 * @param timeoutFuture The future for the timeout task. This future will be cancelled when a channel is acquired.
 */
private void ensureAcquiredChannelIsHealthy(Promise<Channel> delegateFuture,
                                            Promise<Channel> resultFuture,
                                            ScheduledFuture<?> timeoutFuture) {
    // If our delegate failed to connect, forward down the failure. Don't try again.
    if (!delegateFuture.isSuccess()) {
        timeoutFuture.cancel(false);
        resultFuture.tryFailure(delegateFuture.cause());
        return;
    }

    // If our delegate gave us an unhealthy connection, close it and try to get a new one.
    Channel channel = delegateFuture.getNow();
    if (!isHealthy(channel)) {
        channel.close();
        delegate.release(channel);
        tryAcquire(resultFuture, timeoutFuture);
        return;
    }

    // Cancel the timeout (best effort), and return back the healthy channel.
    timeoutFuture.cancel(false);
    if (!resultFuture.trySuccess(channel)) {
        // If we couldn't give the channel to the result future (because it failed for some other reason),
        // just return it to the pool.
        release(channel);
    }
}
 
Example 8
Source File: DnsNameResolver.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static <T> void trySuccess(Promise<T> promise, T result) {
    if (!promise.trySuccess(result)) {
        logger.warn("Failed to notify success ({}) to a promise: {}", result, promise);
    }
}