Example usage for io.netty.util.concurrent Promise setUncancellable

List of usage examples for io.netty.util.concurrent Promise setUncancellable

Introduction

In this page you can find the example usage for io.netty.util.concurrent Promise setUncancellable.

Prototype

boolean setUncancellable();

Source Link

Document

Make this future impossible to cancel.

Usage

From source file:de.unipassau.isl.evs.ssh.app.handler.AbstractAppHandler.java

License:Open Source License

/**
 * Generate a new Future that will track the status of the request contained in the given sent AddressedMessage.
 * If the AddressedMessage couldn't be sent due to an IOException, the returned Future will also fail.
 * Otherwise, the returned Future will complete once {@link #handleResponse(Message.AddressedMessage)} with the
 * according response message is called.
 * <p/>/*from ww  w .j  av a  2  s.  c o m*/
 * <i>The generic return type of this method T must match the payload of the response message.
 * If you are e.g. expecting a {@link de.unipassau.isl.evs.ssh.core.messaging.RoutingKeys#MASTER_LIGHT_GET_REPLY}
 * response, the returned Future should be a Future<LightPayload>. If a error response with an {@link ErrorPayload}
 * is received, the Future will fail the cause being the received ErrorPayload.
 * If the request can have more than one successful response
 * (except for the ErrorPayload, which is always handled as described above and doesn't count here),
 * the most common supertype of both payloads (i.e. MessagePayload) must be declared as generic type for the Future.</i>
 */
protected <T> Future<T> newResponseFuture(final Message.AddressedMessage message) {
    if (!message.getFromID().equals(requireComponent(NamingManager.KEY).getOwnID())) {
        throw new IllegalArgumentException("Can only track messages sent by me");
    }
    final Promise<T> promise = requireComponent(ExecutionServiceComponent.KEY).newPromise();
    final MessageMetrics metrics;
    if (CoreConstants.TRACK_STATISTICS) {
        metrics = new MessageMetrics(message);
        promise.addListener(new FutureListener<T>() {
            @Override
            public void operationComplete(Future<T> future) throws Exception {
                metrics.finished(future);
                Log.v(AbstractAppHandler.this.getClass().getSimpleName() + "-Metrics", metrics.toString());
            }
        });
    } else {
        metrics = null;
    }
    message.getSendFuture().addListener(new FutureListener<Void>() {
        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                promise.setFailure(future.cause());
            } else {
                if (metrics != null) {
                    metrics.sent(future);
                }
            }
        }
    });
    promise.setUncancellable();
    mappings.put(message.getSequenceNr(), promise);
    return promise;
}

From source file:io.airlift.drift.transport.netty.client.ConnectionFactory.java

License:Apache License

@Override
public Future<Channel> getConnection(ConnectionParameters connectionParameters, HostAndPort address) {
    try {// w ww. ja va  2s .  co m
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class)
                .option(ALLOCATOR, allocator)
                .option(CONNECT_TIMEOUT_MILLIS,
                        saturatedCast(connectionParameters.getConnectTimeout().toMillis()))
                .handler(new ThriftClientInitializer(connectionParameters.getTransport(),
                        connectionParameters.getProtocol(), connectionParameters.getMaxFrameSize(),
                        connectionParameters.getRequestTimeout(), connectionParameters.getSocksProxy(),
                        connectionParameters.getSslContextParameters().map(sslContextFactory::get)));

        Promise<Channel> promise = group.next().newPromise();
        promise.setUncancellable();
        bootstrap.connect(new InetSocketAddress(address.getHost(), address.getPort()))
                .addListener((ChannelFutureListener) future -> notifyConnect(future, promise));
        return promise;
    } catch (Throwable e) {
        return group.next().newFailedFuture(new TTransportException(e));
    }
}

From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java

License:Apache License

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);/*from w w w .ja  va 2 s.com*/
    }

    Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise = this.promise;
    if (promise.setUncancellable()) {
        @SuppressWarnings("unchecked")
        AddressedEnvelope<DnsResponse, InetSocketAddress> castResponse = (AddressedEnvelope<DnsResponse, InetSocketAddress>) envelope
                .retain();
        promise.setSuccess(castResponse);
    }
}