Example usage for io.netty.channel ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE

List of usage examples for io.netty.channel ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE

Introduction

In this page you can find the example usage for io.netty.channel ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE.

Prototype

ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE

To view the source code for io.netty.channel ChannelFutureListener FIRE_EXCEPTION_ON_FAILURE.

Click Source Link

Document

A ChannelFutureListener that forwards the Throwable of the ChannelFuture into the ChannelPipeline .

Usage

From source file:com.linkedin.r2.transport.http.client.HttpNettyClient.java

License:Apache License

private void writeRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs,
        final TimeoutTransportCallback<RestResponse> callback) {
    State state = _state.get();/*from  w  w w  .  j ava 2  s .c  o m*/
    if (state != State.RUNNING) {
        errorResponse(callback, new IllegalStateException("Client is " + state));
        return;
    }
    URI uri = request.getURI();
    String scheme = uri.getScheme();
    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        errorResponse(callback,
                new IllegalArgumentException("Unknown scheme: " + scheme + " (only http/https is supported)"));
        return;
    }
    String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        port = "http".equalsIgnoreCase(scheme) ? HTTP_DEFAULT_PORT : HTTPS_DEFAULT_PORT;
    }

    final RestRequest newRequest = new RestRequestBuilder(request)
            .overwriteHeaders(WireAttributeHelper.toWireAttributes(wireAttrs)).build();

    final SocketAddress address;
    try {
        // TODO investigate DNS resolution and timing
        InetAddress inetAddress = InetAddress.getByName(host);
        address = new InetSocketAddress(inetAddress, port);
        requestContext.putLocalAttr(R2Constants.REMOTE_SERVER_ADDR, inetAddress.getHostAddress());
    } catch (UnknownHostException e) {
        errorResponse(callback, e);
        return;
    }

    requestContext.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_1_1);

    final AsyncPool<Channel> pool;
    try {
        pool = _channelPoolManager.getPoolForAddress(address);
    } catch (IllegalStateException e) {
        errorResponse(callback, e);
        return;
    }

    final Cancellable pendingGet = pool.get(new Callback<Channel>() {
        @Override
        public void onSuccess(final Channel channel) {
            // This handler ensures the channel is returned to the pool at the end of the
            // Netty pipeline.
            channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
            callback.addTimeoutTask(new Runnable() {
                @Override
                public void run() {
                    AsyncPool<Channel> pool = channel.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY)
                            .getAndRemove();
                    if (pool != null) {
                        pool.dispose(channel);
                    }
                }
            });

            // This handler invokes the callback with the response once it arrives.
            channel.attr(RAPResponseHandler.CALLBACK_ATTR_KEY).set(callback);

            final State state = _state.get();
            if (state == State.REQUESTS_STOPPING || state == State.SHUTDOWN) {
                // In this case, we acquired a channel from the pool as request processing is halting.
                // The shutdown task might not timeout this callback, since it may already have scanned
                // all the channels for pending requests before we set the callback as the channel
                // attachment.  The TimeoutTransportCallback ensures the user callback in never
                // invoked more than once, so it is safe to invoke it unconditionally.
                errorResponse(callback, new TimeoutException("Operation did not complete before shutdown"));
                return;
            }

            // here we want the exception in outbound operations to be passed back through pipeline so that
            // the user callback would be invoked with the exception and the channel can be put back into the pool
            channel.writeAndFlush(newRequest).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        }

        @Override
        public void onError(Throwable e) {
            errorResponse(callback, e);
        }
    });
    if (pendingGet != null) {
        callback.addTimeoutTask(new Runnable() {
            @Override
            public void run() {
                pendingGet.cancel();
            }
        });
    }
}

From source file:com.mpush.common.message.BaseMessage.java

License:Apache License

public void send() {
    send(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}

From source file:com.mpush.common.message.BaseMessage.java

License:Apache License

public void sendRaw() {
    sendRaw(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}

From source file:com.turn.ttorrent.client.io.PeerClientHandshakeHandler.java

License:Apache License

@Override
protected void process(ChannelHandlerContext ctx, PeerHandshakeMessage message) {
    // We were the connecting client.
    if (!Arrays.equals(infoHash, message.getInfoHash())) {
        logger.warn(/*  w w w  .  j av a 2s  .  c o m*/
                "InfoHash mismatch: requested " + TorrentUtils.toHex(infoHash) + " but received " + message);
        ctx.close().addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        return;
    }

    addPeer(ctx, message, listener);
}

From source file:com.turn.ttorrent.client.io.PeerServerHandshakeHandler.java

License:Apache License

@Override
protected void process(ChannelHandlerContext ctx, PeerHandshakeMessage message) {
    if (LOG.isDebugEnabled())
        LOG.debug("Processing {}", message);
    if (Arrays.equals(message.getPeerId(), torrentProvider.getLocalPeerId())) {
        LOG.warn("Connected to self. Closing.");
        ctx.close().addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        return;/*from www  .  j a v a2 s. c o m*/
    }

    // We are a server.
    TorrentHandler torrent = torrentProvider.getTorrent(message.getInfoHash());
    if (torrent == null) {
        LOG.warn("Unknown torrent {}", message);
        ctx.close().addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        return;
    }
    PeerConnectionListener listener = torrent.getSwarmHandler();
    if (LOG.isTraceEnabled())
        LOG.trace("Found torrent {}", torrent);

    PeerHandshakeMessage response = new PeerHandshakeMessage(torrent.getInfoHash(),
            torrentProvider.getLocalPeerId());
    ctx.writeAndFlush(toByteBuf(ctx, response), ctx.voidPromise());

    addPeer(ctx, message, listener);
}

From source file:com.turn.ttorrent.client.peer.PeerHandler.java

License:Apache License

public void close(@Nonnull String reason) {
    rejectRequestsSent("connection closed: " + reason);
    LOG.debug("{}: Closing {}: {}", new Object[] { getLocalPeerName(), this, reason });
    channel.close().addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}

From source file:com.uber.tchannel.codecs.MessageCodec.java

License:Open Source License

public static ChannelFuture write(ChannelHandlerContext ctx, Frame frame) {
    ChannelFuture f = ctx.writeAndFlush(encode(ctx.alloc(), encode(ctx.alloc(), frame)));
    f.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    return f;//from  www. j  ava 2s  .  c  o  m
}

From source file:io.grpc.alts.internal.TsiHandshakeHandler.java

License:Apache License

/** Sends as many bytes as are available from the handshaker to the remote peer. */
@SuppressWarnings("FutureReturnValueIgnored") // for addListener
private void sendHandshake(ChannelHandlerContext ctx) throws GeneralSecurityException {
    while (true) {
        boolean written = false;
        ByteBuf buf = ctx.alloc().buffer(HANDSHAKE_FRAME_SIZE).retain(); // refcnt = 2
        try {// w ww  .  j  a  v a  2  s  . c  o  m
            handshaker.getBytesToSendToPeer(buf);
            if (buf.isReadable()) {
                ctx.writeAndFlush(buf).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
                written = true;
            } else {
                break;
            }
        } catch (GeneralSecurityException e) {
            throw new GeneralSecurityException("TsiHandshakeHandler encountered exception", e);
        } finally {
            buf.release(written ? 1 : 2);
        }
    }
}

From source file:io.jsync.impl.ExceptionDispatchHandler.java

License:Open Source License

@Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise)
        throws Exception {
    ctx.bind(localAddress, promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE));
}

From source file:io.jsync.impl.ExceptionDispatchHandler.java

License:Open Source License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) throws Exception {
    ctx.connect(remoteAddress, localAddress,
            promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE));
}