List of usage examples for io.netty.channel ChannelFuture cause
Throwable cause();
From source file:com.spotify.heroic.consumer.collectd.Server.java
License:Apache License
public static AsyncFuture<Server> setup(final AsyncFramework async, final CollectdChannelHandler handler, final InetAddress host, final int port) { final EventLoopGroup group = new NioEventLoopGroup(); final Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(handler); final ResolvableFuture<Server> future = async.future(); b.bind(host, port).addListener(new ChannelFutureListener() { @Override/*from w w w . j a v a2 s . c o m*/ public void operationComplete(final ChannelFuture f) throws Exception { if (f.isSuccess()) { future.resolve(new Server(async, f.channel())); } else { future.fail(f.cause() != null ? f.cause() : new RuntimeException("Failed to bind")); } } }); return future; }
From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java
License:Apache License
private <R> ChannelFutureListener handleConnect(final NativeRpcRequest request, final ResolvableFuture<R> future, final AtomicReference<Timeout> heartbeatTimeout, final Timeout requestTimeout) { return new ChannelFutureListener() { @Override/*from w ww. j av a 2s .c o m*/ public void operationComplete(ChannelFuture f) throws Exception { if (!f.isSuccess()) { future.fail(f.cause()); return; } f.channel().writeAndFlush(request) .addListener(handleRequestSent(future, heartbeatTimeout, requestTimeout)); } }; }
From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcClient.java
License:Apache License
private <R> ChannelFutureListener handleRequestSent(final ResolvableFuture<R> future, final AtomicReference<Timeout> heartbeatTimeout, final Timeout requestTimeout) { return new ChannelFutureListener() { @Override//w w w. ja va2 s . c o m public void operationComplete(ChannelFuture f) throws Exception { requestTimeout.cancel(); if (!f.isSuccess()) { future.fail(f.cause()); return; } final Timeout timeout = timer.newTimeout(heartbeatTimeout(f.channel(), future), heartbeatInterval, TimeUnit.MILLISECONDS); heartbeatTimeout.set(timeout); } }; }
From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcProtocolServer.java
License:Apache License
private AsyncFuture<Void> start() { final ServerBootstrap s = new ServerBootstrap(); s.channel(NioServerSocketChannel.class); s.group(bossGroup, workerGroup);/*from ww w . j a va2s. c o m*/ s.childHandler(new NativeRpcServerSession(timer, mapper, container, maxFrameSize, encoding)); final ChannelFuture bind = s.bind(address); bind.addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture f) throws Exception { if (!f.isSuccess()) { bindFuture.fail(f.cause()); return; } serverChannel.set(f.channel()); final InetSocketAddress address = (InetSocketAddress) f.channel().localAddress(); bindFuture.resolve(address); } }); return bindFuture.directTransform(a -> null); }
From source file:com.test.AbstractBootstrap.java
License:Apache License
private ChannelFuture doBind(final SocketAddress localAddress) { final ChannelFuture regFuture = initAndRegister(); final Channel channel = regFuture.channel(); if (regFuture.cause() != null) { return regFuture; }// w w w. jav a 2 s.c om if (regFuture.isDone()) { // At this point we know that the registration was complete and successful. ChannelPromise promise = channel.newPromise(); doBind0(regFuture, channel, localAddress, promise); return promise; } else { // Registration future is almost always fulfilled already, but just in case it's not. final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel); regFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { Throwable cause = future.cause(); if (cause != null) { // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an // IllegalStateException once we try to access the EventLoop of the Channel. promise.setFailure(cause); } else { // Registration was successful, so set the correct executor to use. // See https://github.com/netty/netty/issues/2586 promise.executor = channel.eventLoop(); } doBind0(regFuture, channel, localAddress, promise); } }); return promise; } }
From source file:com.test.AbstractBootstrap.java
License:Apache License
final ChannelFuture initAndRegister() { final Channel channel = channelFactory().newChannel(); try {/*from w w w.j a v a 2 s . com*/ init(channel); } catch (Throwable t) { channel.unsafe().closeForcibly(); // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t); } ChannelFuture regFuture = group().register(channel); if (regFuture.cause() != null) { if (channel.isRegistered()) { channel.close(); } else { channel.unsafe().closeForcibly(); } } // If we are here and the promise is not failed, it's one of the following cases: // 1) If we attempted registration from the event loop, the registration has been completed at this point. // i.e. It's safe to attempt bind() or connect() now because the channel has been registered. // 2) If we attempted registration from the other thread, the registration request has been successfully // added to the event loop's task queue for later execution. // i.e. It's safe to attempt bind() or connect() now: // because bind() or connect() will be executed *after* the scheduled registration task is executed // because register(), bind(), and connect() are all bound to the same thread. return regFuture; }
From source file:com.test.AbstractBootstrap.java
License:Apache License
private static void doBind0(final ChannelFuture regFuture, final Channel channel, final SocketAddress localAddress, final ChannelPromise promise) { // This method is invoked before channelRegistered() is triggered. Give user handlers a chance to set up // the pipeline in its channelRegistered() implementation. channel.eventLoop().execute(new Runnable() { @Override//from w w w.j a v a2 s . c om public void run() { if (regFuture.isSuccess()) { channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); } else { promise.setFailure(regFuture.cause()); } } }); }
From source file:com.tongbanjie.tarzan.rpc.netty.NettyRpcAbstract.java
License:Apache License
public RpcCommand invokeSyncImpl(final Channel channel, final RpcCommand request, final long timeoutMillis) throws InterruptedException, RpcTooMuchRequestException, RpcSendRequestException, RpcTimeoutException { //check channel writable if (!channel.isWritable()) { throw new RpcTooMuchRequestException( String.format("Invoke request too much, the channel[%s] is not writable", channel.toString())); }/*ww w. j a v a2 s.c om*/ final int opaque = request.getOpaque(); try { final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null); this.responseTable.put(opaque, responseFuture); final SocketAddress addr = channel.remoteAddress(); channel.writeAndFlush(request).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture f) throws Exception { if (f.isSuccess()) { responseFuture.setSendRequestOK(true); return; } else { responseFuture.setSendRequestOK(false); } responseTable.remove(opaque); responseFuture.setCause(f.cause()); responseFuture.putResponse(null); LOGGER.warn("send a request command to channel <" + addr + "> failed."); } }); RpcCommand responseCommand = responseFuture.waitResponse(timeoutMillis); if (null == responseCommand) { if (responseFuture.isSendRequestOK()) { throw new RpcTimeoutException(RpcHelper.parseSocketAddressAddr(addr), timeoutMillis, responseFuture.getCause()); } else { throw new RpcSendRequestException(RpcHelper.parseSocketAddressAddr(addr), responseFuture.getCause()); } } return responseCommand; } finally { this.responseTable.remove(opaque); } }
From source file:com.tongbanjie.tarzan.rpc.netty.NettyRpcClient.java
License:Apache License
private Channel createChannel(final String addr) throws InterruptedException { ChannelWrapper cw = this.channelTables.get(addr); if (cw != null && cw.isOK()) { return cw.getChannel(); }/* w ww . j av a 2 s .co m*/ if (this.lockChannelTables.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) { try { boolean createNewConnection = false; cw = this.channelTables.get(addr); if (cw != null) { if (cw.isOK()) { return cw.getChannel(); } else if (!cw.getChannelFuture().isDone()) { createNewConnection = false; } else { this.channelTables.remove(addr); createNewConnection = true; } } else { createNewConnection = true; } if (createNewConnection) { ChannelFuture channelFuture = this.bootstrap.connect(RpcHelper.string2SocketAddress(addr)); LOGGER.info("createChannel: begin to connect remote host[{}] asynchronously", addr); cw = new ChannelWrapper(channelFuture); this.channelTables.put(addr, cw); } } catch (Exception e) { LOGGER.error("createChannel: create channel exception", e); } finally { this.lockChannelTables.unlock(); } } else { LOGGER.warn("createChannel: try to lock channel table, but timeout, {}ms", LOCK_TIMEOUT_MILLIS); } if (cw != null) { ChannelFuture channelFuture = cw.getChannelFuture(); if (channelFuture.awaitUninterruptibly(this.nettyClientConfig.getConnectTimeoutMillis())) { if (cw.isOK()) { LOGGER.info("createChannel: connect remote host[{}] success, {}", addr, channelFuture.toString()); return cw.getChannel(); } else { LOGGER.warn( "createChannel: connect remote host[" + addr + "] failed, " + channelFuture.toString(), channelFuture.cause()); } } else { LOGGER.warn("createChannel: connect remote host[{}] timeout {}ms, {}", addr, this.nettyClientConfig.getConnectTimeoutMillis(), channelFuture.toString()); } } return null; }
From source file:com.turo.pushy.apns.ApnsClientHandler.java
License:Open Source License
@Override public void userEventTriggered(final ChannelHandlerContext context, final Object event) throws Exception { if (event instanceof IdleStateEvent) { log.trace("Sending ping due to inactivity."); this.encoder().writePing(context, false, System.currentTimeMillis(), context.newPromise()) .addListener(new GenericFutureListener<ChannelFuture>() { @Override// w ww . j a va2 s.co m public void operationComplete(final ChannelFuture future) { if (!future.isSuccess()) { log.debug("Failed to write PING frame.", future.cause()); future.channel().close(); } } }); this.pingTimeoutFuture = context.channel().eventLoop().schedule(new Runnable() { @Override public void run() { log.debug("Closing channel due to ping timeout."); context.channel().close(); } }, pingTimeoutMillis, TimeUnit.MILLISECONDS); this.flush(context); } super.userEventTriggered(context, event); }