List of usage examples for io.netty.channel ChannelFuture cause
Throwable cause();
From source file:http.HTTPResponseHandler.java
License:Open Source License
/** * Provide asynchronous response to HTTP2 request * * @param streamId StreamID/* w w w. ja v a2 s . c om*/ * @return Response string */ public String getResponse(int streamId) { String message = streamIdResponseMap.get(streamId); if (message != null) { return message; } else { Entry<ChannelFuture, ChannelPromise> channelFutureChannelPromiseEntry = streamIdPromiseMap .get(streamId); if (channelFutureChannelPromiseEntry != null) { ChannelFuture writeFuture = channelFutureChannelPromiseEntry.getKey(); if (!writeFuture.awaitUninterruptibly(ServerUtil.HTTP2_RESPONSE_TIME_OUT, ServerUtil.HTTP2_RESPONSE_TIME_UNIT)) { streamIdPromiseMap.remove(streamId); throw new IllegalStateException("Timed out waiting to write for stream id " + streamId); } if (!writeFuture.isSuccess()) { streamIdPromiseMap.remove(streamId); throw new RuntimeException(writeFuture.cause()); } ChannelPromise promise = channelFutureChannelPromiseEntry.getValue(); if (!promise.awaitUninterruptibly(ServerUtil.HTTP2_RESPONSE_TIME_OUT, ServerUtil.HTTP2_RESPONSE_TIME_UNIT)) { streamIdPromiseMap.remove(streamId); throw new IllegalStateException("Timed out waiting for response on stream id " + streamId); } if (!promise.isSuccess()) { streamIdPromiseMap.remove(streamId); throw new RuntimeException(promise.cause()); } } } return streamIdResponseMap.get(streamId); }
From source file:http2.client.HttpResponseHandler.java
License:Apache License
/** * anticipated : ?//from w w w. j a v a 2 s . c o m * Wait (sequentially) for a time duration for each anticipated response * * @param timeout Value of time to wait for each response * @param unit Units associated with {@code timeout} * @see HttpResponseHandler#put(int, ChannelFuture, ChannelPromise) */ public void awaitResponses(long timeout, TimeUnit unit) { Iterator<Entry<Integer, Entry<ChannelFuture, ChannelPromise>>> itr = streamidPromiseMap.entrySet() .iterator(); while (itr.hasNext()) { Entry<Integer, Entry<ChannelFuture, ChannelPromise>> entry = itr.next(); ChannelFuture writeFuture = entry.getValue().getKey(); // if (!writeFuture.awaitUninterruptibly(timeout, unit)) { throw new IllegalStateException("Timed out waiting to write for stream id " + entry.getKey()); } // if (!writeFuture.isSuccess()) { throw new RuntimeException(writeFuture.cause()); } // ChannelPromise promise = entry.getValue().getValue(); if (!promise.awaitUninterruptibly(timeout, unit)) { throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey()); } if (!promise.isSuccess()) { throw new RuntimeException(promise.cause()); } System.out.println("---Stream id: " + entry.getKey() + " received---"); itr.remove(); } }
From source file:io.advantageous.conekt.datagram.impl.DatagramChannelFutureListener.java
License:Open Source License
private void notifyHandler(ChannelFuture future) { if (future.isSuccess()) { handler.handle(Future.succeededFuture(result)); } else {//from ww w . j av a 2s . c om handler.handle(Future.failedFuture(future.cause())); } }
From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java
License:Open Source License
private void internalConnect(ContextImpl clientContext, int port, String host, Handler<ClientConnection> connectHandler, Handler<Throwable> connectErrorHandler, ConnectionLifeCycleListener listener) { ContextImpl context;// ww w . j av a 2 s. com if (clientContext == null) { // Embedded context = vertx.getOrCreateContext(); } else { context = clientContext; } Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.nettyEventLoop()); bootstrap.channelFactory(new VertxNioSocketChannelFactory()); sslHelper.validate(vertx); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (options.isSsl()) { pipeline.addLast("ssl", sslHelper.createSslHandler(vertx, true, host, port)); } pipeline.addLast("codec", new HttpClientCodec(4096, 8192, options.getMaxChunkSize(), false, false)); if (options.isTryUseCompression()) { pipeline.addLast("inflater", new HttpContentDecompressor(true)); } if (options.getIdleTimeout() > 0) { pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout())); } pipeline.addLast("handler", new ClientHandler(vertx, context)); } }); applyConnectionOptions(bootstrap); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener((ChannelFuture channelFuture) -> { Channel ch = channelFuture.channel(); if (channelFuture.isSuccess()) { if (options.isSsl()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = ch.pipeline().get(SslHandler.class); io.netty.util.concurrent.Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(fut2 -> { if (fut2.isSuccess()) { connected(context, port, host, ch, connectHandler, connectErrorHandler, listener); } else { SSLHandshakeException sslException = new SSLHandshakeException( "Failed to create SSL connection"); Optional.ofNullable(fut2.cause()).ifPresent(sslException::initCause); connectionFailed(context, ch, connectErrorHandler, sslException, listener); } }); } else { connected(context, port, host, ch, connectHandler, connectErrorHandler, listener); } } else { connectionFailed(context, ch, connectErrorHandler, channelFuture.cause(), listener); } }); }
From source file:io.advantageous.conekt.net.impl.NetClientImpl.java
License:Open Source License
private void connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler, int remainingAttempts) { Objects.requireNonNull(host, "No null host accepted"); Objects.requireNonNull(connectHandler, "No null connectHandler accepted"); ContextImpl context = vertx.getOrCreateContext(); sslHelper.validate(vertx);// w w w . j a v a2 s . co m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.nettyEventLoop()); bootstrap.channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (sslHelper.isSSL()) { SslHandler sslHandler = sslHelper.createSslHandler(vertx, true); pipeline.addLast("ssl", sslHandler); } if (sslHelper.isSSL()) { // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used. pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); // For large file / sendfile support } if (options.getIdleTimeout() > 0) { pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout())); } pipeline.addLast("handler", new ConektNetHandler(socketMap)); } }); applyConnectionOptions(bootstrap); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener((ChannelFuture channelFuture) -> { Channel ch = channelFuture.channel(); if (channelFuture.isSuccess()) { if (sslHelper.isSSL()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = ch.pipeline().get(SslHandler.class); io.netty.util.concurrent.Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(future2 -> { if (future2.isSuccess()) { connected(context, ch, connectHandler); } else { failed(context, ch, future2.cause(), connectHandler); } }); } else { connected(context, ch, connectHandler); } } else { if (remainingAttempts > 0 || remainingAttempts == -1) { context.executeFromIO(() -> { log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds"); //Set a timer to retry connection vertx.setTimer(options.getReconnectInterval(), tid -> connect(port, host, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1)); }); } else { failed(context, ch, channelFuture.cause(), connectHandler); } } }); }
From source file:io.advantageous.conekt.net.impl.NetSocketImpl.java
License:Open Source License
@Override public NetSocket sendFile(String filename, long offset, long length, final Handler<AsyncResult<Void>> resultHandler) { File f = vertx.resolveFile(filename); if (f.isDirectory()) { throw new IllegalArgumentException("filename must point to a file and not to a directory"); }//from w w w. j ava 2 s .com RandomAccessFile raf = null; try { raf = new RandomAccessFile(f, "r"); ChannelFuture future = super.sendFile(raf, Math.min(offset, f.length()), Math.min(length, f.length() - offset)); if (resultHandler != null) { future.addListener(fut -> { final AsyncResult<Void> res; if (future.isSuccess()) { res = Future.succeededFuture(); } else { res = Future.failedFuture(future.cause()); } vertx.runOnContext(v -> resultHandler.handle(res)); }); } } catch (IOException e) { try { if (raf != null) { raf.close(); } } catch (IOException ignore) { } if (resultHandler != null) { vertx.runOnContext(v -> resultHandler.handle(Future.failedFuture(e))); } else { log.error("Failed to send file", e); } } return this; }
From source file:io.airlift.drift.transport.netty.client.ConnectionFactory.java
License:Apache License
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();/*w w w . ja va 2 s.c o m*/ } } else { promise.tryFailure(future.cause()); } }
From source file:io.airlift.drift.transport.netty.client.ThriftClientHandler.java
License:Apache License
private void messageSent(ChannelHandlerContext context, ChannelFuture future, RequestHandler requestHandler) { try {//from w w w. ja v a 2s. c o m if (!future.isSuccess()) { onError(context, new TTransportException("Sending request failed", future.cause()), Optional.of(requestHandler)); return; } requestHandler.onRequestSent(); } catch (Throwable t) { onError(context, t, Optional.of(requestHandler)); } }
From source file:io.airlift.drift.transport.netty.ThriftClientHandler.java
License:Apache License
private void messageSent(ChannelHandlerContext context, ChannelFuture future, RequestHandler requestHandler) { try {//from ww w . ja va2 s . com if (!future.isSuccess()) { onError(context, new TTransportException("Sending request failed", future.cause())); return; } requestHandler.onRequestSent(); } catch (Throwable t) { onError(context, t); } }
From source file:io.apigee.trireme.container.netty.NettyHttpFuture.java
License:Open Source License
@Override public void operationComplete(ChannelFuture future) { Throwable cause = future.cause(); if ((cause != null) || (cause instanceof ClosedChannelException)) { invokeListener(false, true, cause); } else {/*from www . jav a 2 s . com*/ invokeListener(future.isSuccess(), false, future.cause()); } }