List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
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 w w w . ja v a 2 s .com*/ 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;/*from w ww .ja v a 2s.c om*/ 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 .ja v a 2s .c o 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"); }/* w ww .j a v a 2s. c om*/ 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();/*www .j a v a 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. j av a 2s .co 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 w w w. j ava2s. co m*/ 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 w w w. j ava 2 s. c o m invokeListener(future.isSuccess(), false, future.cause()); } }
From source file:io.codis.nedis.NedisClientBuilder.java
License:Apache License
public Future<NedisClientImpl> connect(SocketAddress remoteAddress) { validateGroupConfig();/* w w w. jav a 2 s .co m*/ Bootstrap b = new Bootstrap().group(group).channel(channelClass).handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new RedisResponseDecoder(), new RedisDuplexHandler(TimeUnit.MILLISECONDS.toNanos(timeoutMs))); } }); if (timeoutMs > 0) { b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) Math.min(Integer.MAX_VALUE, timeoutMs)); } ChannelFuture f = b.connect(remoteAddress); final Promise<NedisClientImpl> promise = f.channel().eventLoop().newPromise(); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { promise.trySuccess(new NedisClientImpl(future.channel(), pool)); } else { promise.tryFailure(future.cause()); } } }); return promise; }
From source file:io.dyn.net.tcp.TcpClient.java
License:Apache License
@SuppressWarnings({ "unchecked" }) @Override// ww w. j a v a2 s . c o m public T start() { if (!started.get()) { on(Lifecycle.STOP, new CompletionHandler() { @Override protected void complete() { if (channelFuture.awaitUninterruptibly(15, TimeUnit.SECONDS)) { channelFuture.getChannel().close(); } started.set(false); } }); bootstrap.setOption("child.keepAlive", keepAlive); bootstrap.setOption("child.receiveBufferSize", Buffer.SMALL_BUFFER_SIZE); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { final ChannelPipeline pipeline = Channels.pipeline(); TcpClient.this.configurePipeline(pipeline); return pipeline; } }); try { channelFuture = bootstrap.connect(new InetSocketAddress(InetAddress.getByName(host), port)); channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { started.set(true); event(Lifecycle.START); } else { Throwable t = channelFuture.getCause(); event(Events.classToEventExpression(t.getClass()), t); } } }); } catch (UnknownHostException e) { event(Events.classToEventExpression(e.getClass()), e); } } return (T) this; }