List of usage examples for io.netty.channel ChannelFuture isSuccess
boolean isSuccess();
From source file:code.google.nfs.rpc.netty.client.NettyClient.java
License:Apache License
public void sendRequest(final RequestWrapper wrapper, final int timeout) throws Exception { final long beginTime = System.currentTimeMillis(); final Client self = this; ChannelFuture writeFuture = cf.channel().writeAndFlush(wrapper); // use listener to avoid wait for write & thread context switch writeFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { return; }//from w ww . j ava2 s. c o m String errorMsg = ""; // write timeout if (System.currentTimeMillis() - beginTime >= timeout) { errorMsg = "write to send buffer consume too long time(" + (System.currentTimeMillis() - beginTime) + "),request id is:" + wrapper.getId(); } if (future.isCancelled()) { errorMsg = "Send request to " + cf.channel().toString() + " cancelled by user,request id is:" + wrapper.getId(); } if (!future.isSuccess()) { if (cf.channel().isOpen()) { // maybe some exception,so close the channel cf.channel().close(); } else { NettyClientFactory.getInstance().removeClient(key, self); } errorMsg = "Send request to " + cf.channel().toString() + " error" + future.cause(); } LOGGER.error(errorMsg); ResponseWrapper response = new ResponseWrapper(wrapper.getId(), wrapper.getCodecType(), wrapper.getProtocolType()); response.setException(new Exception(errorMsg)); self.putResponse(response); } }); }
From source file:code.google.nfs.rpc.netty.client.NettyClientFactory.java
License:Apache License
protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.TCP_NODELAY, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true"))) .option(ChannelOption.SO_REUSEADDR, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true"))); if (connectTimeout < 1000) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000); } else {/*w w w .j a v a 2s.c om*/ bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout); } final NettyClientHandler handler = new NettyClientHandler(this, key); bootstrap.handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("decoder", new NettyProtocolDecoder()); pipeline.addLast("encoder", new NettyProtocolEncoder()); pipeline.addLast("handler", handler); } }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort)).sync(); future.awaitUninterruptibly(connectTimeout); if (!future.isDone()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!"); } if (future.isCancelled()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); } if (!future.isSuccess()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); } NettyClient client = new NettyClient(future, key, connectTimeout); handler.setClient(client); return client; }
From source file:code.google.nfs.rpc.netty.server.NettyServerHandler.java
License:Apache License
private void sendErrorResponse(final ChannelHandlerContext ctx, final RequestWrapper request) { ResponseWrapper responseWrapper = new ResponseWrapper(request.getId(), request.getCodecType(), request.getProtocolType());// w w w . j a v a 2 s . c o m responseWrapper.setException( new Exception("server threadpool full,maybe because server is slow or too many requests")); ChannelFuture wf = ctx.channel().writeAndFlush(responseWrapper); wf.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { LOGGER.error("server write response error,request id is: " + request.getId()); } } }); }
From source file:code.google.nfs.rpc.netty4.client.Netty4Client.java
License:Apache License
public void sendRequest(final RequestWrapper wrapper, final int timeout) throws Exception { final long beginTime = System.currentTimeMillis(); final Client self = this; ChannelFuture writeFuture = cf.channel().writeAndFlush(wrapper); // use listener to avoid wait for write & thread context switch writeFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { return; }// w w w . ja va 2 s . co m String errorMsg = ""; // write timeout if (System.currentTimeMillis() - beginTime >= timeout) { errorMsg = "write to send buffer consume too long time(" + (System.currentTimeMillis() - beginTime) + "),request id is:" + wrapper.getId(); } if (future.isCancelled()) { errorMsg = "Send request to " + cf.channel().toString() + " cancelled by user,request id is:" + wrapper.getId(); } if (!future.isSuccess()) { if (cf.channel().isActive()) { // maybe some exception,so close the channel cf.channel().close(); } else { Netty4ClientFactory.getInstance().removeClient(key, self); } errorMsg = "Send request to " + cf.channel().toString() + " error" + future.cause(); } LOGGER.error(errorMsg); ResponseWrapper response = new ResponseWrapper(wrapper.getId(), wrapper.getCodecType(), wrapper.getProtocolType()); response.setException(new Exception(errorMsg)); self.putResponse(response); } }); }
From source file:code.google.nfs.rpc.netty4.client.Netty4ClientFactory.java
License:Apache License
protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception { final Netty4ClientHandler handler = new Netty4ClientHandler(this, key); EventLoopGroup group = new NioEventLoopGroup(1); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true"))) .option(ChannelOption.SO_REUSEADDR, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true"))) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout < 1000 ? 1000 : connectTimeout) .handler(new ChannelInitializer<SocketChannel>() { @Override/*from ww w . ja v a 2 s . co m*/ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new Netty4ProtocolDecoder()); ch.pipeline().addLast("encoder", new Netty4ProtocolEncoder()); ch.pipeline().addLast("handler", handler); } }); ChannelFuture future = b.connect(targetIP, targetPort); future.awaitUninterruptibly(connectTimeout); if (!future.isDone()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!"); } if (future.isCancelled()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); } if (!future.isSuccess()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); } Netty4Client client = new Netty4Client(future, key, connectTimeout); handler.setClient(client); return client; }
From source file:code.google.nfs.rpc.netty4.server.Netty4ServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { RequestWrapper request = (RequestWrapper) msg; long beginTime = System.currentTimeMillis(); ResponseWrapper responseWrapper = ProtocolFactory.getServerHandler(request.getProtocolType()) .handleRequest(request);/*from ww w .ja va 2 s . c o m*/ final int id = request.getId(); // already timeout,so not return if ((System.currentTimeMillis() - beginTime) >= request.getTimeout()) { LOGGER.warn("timeout,so give up send response to client,requestId is:" + id + ",client is:" + ctx.channel().remoteAddress() + ",consumetime is:" + (System.currentTimeMillis() - beginTime) + ",timeout is:" + request.getTimeout()); return; } ChannelFuture wf = ctx.channel().writeAndFlush(responseWrapper); wf.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { LOGGER.error("server write response error,request id is: " + id); } } }); }
From source file:com.addthis.hydra.query.aggregate.MeshSourceAggregator.java
License:Apache License
@Override public void operationComplete(ChannelFuture future) throws Exception { // make sure this auto-recurring task doesn't go on forever if (stragglerTaskFuture != null) { stragglerTaskFuture.cancel(true); }/*from ww w. j a v a 2 s .co m*/ if (!future.isSuccess()) { stopSources(future.cause().getMessage()); meshQueryMaster.handleError(query); consumer.sourceError(promoteHackForThrowables(future.cause())); } else { safelyRemoveSelfFromPipeline(future); stopSources("query is complete"); consumer.sendComplete(); } }
From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java
License:Apache License
@Override public void operationComplete(ChannelFuture future) throws Exception { log.trace("complete called"); if (!future.isSuccess()) { safelyHandleQueryFailure(future); }/* www . j a va2s . com*/ // schedule next query poll executor.execute(this); log.trace("rescheduled"); }
From source file:com.addthis.meshy.AggregateChannelFuture.java
License:Apache License
/** the promises collection should not be mutated after construction */ AggregateChannelFuture(Collection<ChannelFuture> futures, EventExecutor executor) { super(executor); this.futures = futures; this.complete = new AtomicInteger(0); this.aggregatingListener = future -> { if (!future.isSuccess()) { anyCause = future.cause();//w w w . ja va 2 s . c om } if (complete.incrementAndGet() == futures.size()) { if (anyCause == null) { super.setSuccess(null); } else { super.setFailure(anyCause); } } }; for (ChannelFuture future : futures) { future.addListener(aggregatingListener); } }
From source file:com.addthis.meshy.MeshyClient.java
License:Apache License
/** * client/* w w w . j a va2 s. c o m*/ */ public MeshyClient(InetSocketAddress address) throws IOException { super(); /* block session creation until connection is fully established */ try { clientInitGate.acquire(); } catch (Exception ex) { throw new RuntimeException(ex); } ChannelFuture clientConnect = connect(address); clientConnect.awaitUninterruptibly(); if (!clientConnect.isSuccess()) { close(); throw new IOException("connection fail to " + address); } clientChannelCloseFuture = clientConnect.channel().closeFuture(); /* re-acquire after connection comes up, which releases the lock */ try { clientInitGate.acquire(); } catch (Exception ex) { throw new RuntimeException(ex); } if (log.isDebugEnabled()) { log.debug("client [{}] connected to {}", getUUID(), address); } }