io.netty.channel.ChannelFuture.await()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(294)

本文整理了Java中io.netty.channel.ChannelFuture.await()方法的一些代码示例,展示了ChannelFuture.await()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ChannelFuture.await()方法的具体详情如下:
包路径:io.netty.channel.ChannelFuture
类名称:ChannelFuture
方法名:await

ChannelFuture.await介绍

暂无

代码示例

代码示例来源:origin: ethereum/ethereumj

public void close() {
  logger.info("Closing UDPListener...");
  shutdown = true;
  if (channel != null) {
    try {
      channel.close().await(10, TimeUnit.SECONDS);
    } catch (Exception e) {
      logger.warn("Problems closing UDPListener", e);
    }
  }
  if (discoveryExecutor != null) {
    try {
      discoveryExecutor.close();
    } catch (Exception e) {
      logger.warn("Problems closing DiscoveryExecutor", e);
    }
  }
}

代码示例来源:origin: qunarcorp/qmq

public void start() {
  bootstrap.option(ChannelOption.SO_REUSEADDR, true);
  bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
  bootstrap.group(bossGroup, workerGroup)
      .channel(NioServerSocketChannel.class)
      .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) {
          ch.pipeline().addLast("connectionHandler", connectionHandler);
          ch.pipeline().addLast("encoder", new EncodeHandler());
          ch.pipeline().addLast("decoder", new DecodeHandler(true));
          ch.pipeline().addLast("dispatcher", serverHandler);
        }
      });
  try {
    channel = bootstrap.bind(port).await().channel();
  } catch (InterruptedException e) {
    LOG.error("server start fail", e);
  }
  LOG.info("listen on port {}", port);
}

代码示例来源:origin: Netflix/zuul

/**
   * Note this blocks until all the channels have finished closing.
   */
  public void gracefullyShutdownClientChannels()
  {
    LOG.warn("Gracefully shutting down all client channels");
    try {
      List<ChannelFuture> futures = new ArrayList<>();
      channels.forEach(channel -> {
        ConnectionCloseType.setForChannel(channel, ConnectionCloseType.DELAYED_GRACEFUL);
        ChannelFuture f = channel.pipeline().close();
        futures.add(f);
      });

      LOG.warn("Waiting for " + futures.size() + " client channels to be closed.");
      for (ChannelFuture f : futures) {
        f.await();
      }
      LOG.warn(futures.size() + " client channels closed.");
    }
    catch (InterruptedException ie) {
      LOG.warn("Interrupted while shutting down client channels");
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void send(Object message, boolean sent) throws RemotingException {
  super.send(message, sent);
  boolean success = true;
  int timeout = 0;
  try {
    ChannelFuture future = channel.writeAndFlush(message);
    if (sent) {
      timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
      success = future.await(timeout);
    }
    Throwable cause = future.cause();
    if (cause != null) {
      throw cause;
    }
  } catch (Throwable e) {
    throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
  }
  if (!success) {
    throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
        + "in timeout(" + timeout + "ms) limit");
  }
}

代码示例来源:origin: alibaba/fescar

/**
 * Gets new channel.
 *
 * @param address the address
 * @return the new channel
 */
protected Channel getNewChannel(InetSocketAddress address) {
  Channel channel = null;
  ChannelFuture f = this.bootstrap.connect(address);
  try {
    f.await(this.nettyClientConfig.getConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
    if (f.isCancelled()) {
      throw new FrameworkException(f.cause(), "connect cancelled, can not connect to fescar-server.");
    } else if (!f.isSuccess()) {
      throw new FrameworkException(f.cause(), "connect failed, can not connect to fescar-server.");
    } else {
      channel = f.channel();
    }
  } catch (Exception e) {
    throw new FrameworkException(e, "can not connect to fescar-server.");
  }
  return channel;
}

代码示例来源:origin: Netflix/zuul

/**
   * Note this blocks until all the channels have finished closing.
   */
  public void gracefullyShutdownClientChannels()
  {
    LOG.warn("Gracefully shutting down all client channels");
    try {
      List<ChannelFuture> futures = new ArrayList<>();
      channels.forEach(channel -> {
        ConnectionCloseType.setForChannel(channel, ConnectionCloseType.DELAYED_GRACEFUL);
        ChannelFuture f = channel.pipeline().close();
        futures.add(f);
      });

      LOG.warn("Waiting for " + futures.size() + " client channels to be closed.");
      for (ChannelFuture f : futures) {
        f.await();
      }
      LOG.warn(futures.size() + " client channels closed.");
    }
    catch (InterruptedException ie) {
      LOG.warn("Interrupted while shutting down client channels");
    }
  }
}

代码示例来源:origin: alipay/sofa-rpc

channelFuture.await();
if (channelFuture.isSuccess()) {
  flag = Boolean.TRUE;

代码示例来源:origin: alipay/sofa-rpc

channelFuture.await();
if (channelFuture.isSuccess()) {
  flag = Boolean.TRUE;

代码示例来源:origin: ethereum/ethereumj

public void startClient()
    throws InterruptedException {
  String defaultMessage = RandomStringUtils.randomAlphanumeric(MAX_LENGTH);
  for (int i = defaultMessage.length() - 1; i >= 0 ; i--) {
    int sendAttempts = 0;
    boolean ok = false;
    while (sendAttempts < 3) {
      Channel channel = create(clientAddr, clientPort);
      String sendMessage = defaultMessage.substring(i, defaultMessage.length());
      FindNodeMessage msg = FindNodeMessage.create(sendMessage.getBytes(), privKey);
      System.out.printf("Sending message with string payload of size %s, packet size %s, attempt %s%n", sendMessage.length(), msg.getPacket().length, sendAttempts + 1);
      nodeManager.getMessageSender().sendPacket(msg.getPacket(), new InetSocketAddress(serverAddr, serverPort));
      ok = channel.closeFuture().await(1, TimeUnit.SECONDS);
      if (ok) break;
      sendAttempts++;
      channel.close().sync();
    }
    if (!ok) {
      System.out.println("ERROR: Timeout waiting for response after all attempts");
      assert false;
    } else {
      System.out.println("OK");
    }
  }
}

代码示例来源:origin: rackerlabs/blueflood

@VisibleForTesting
public void stopServer() {
  try {
    serverChannel.close().await(5, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
    // Pass
  }
  acceptorGroup.shutdownGracefully();
  workerGroup.shutdownGracefully();
}

代码示例来源:origin: jwpttcg66/NettyGameServer

channelFuture.await();
} catch (InterruptedException e) {
  logger.error(e.toString(), e);

代码示例来源:origin: org.apache.hadoop/hadoop-hdfs

/**
 * Start WebImageViewer and wait until the thread is interrupted.
 * @param fsimage the fsimage to load.
 * @throws IOException if failed to load the fsimage.
 * @throws RuntimeException if security is enabled in configuration.
 */
public void start(String fsimage) throws IOException {
 try {
  if (UserGroupInformation.isSecurityEnabled()) {
   throw new RuntimeException(
     "WebImageViewer does not support secure mode. To start in " +
       "non-secure mode, pass -D" +
       CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION +
       "=simple");
  }
  initServer(fsimage);
  channel.closeFuture().await();
 } catch (InterruptedException e) {
  LOG.info("Interrupted. Stopping the WebImageViewer.");
  close();
 }
}

代码示例来源:origin: hengyunabc/xdiamond

try {
 boolean await = future.await(10, TimeUnit.SECONDS);
 if (await && future.isSuccess()) {
  Future<List<ResolvedConfigVO>> configFuture =

代码示例来源:origin: SeanDragon/protools

public HttpReceive request(HttpSend httpSend, long timeout, TimeUnit timeUnit) {
  final HttpReceive httpReceive = new HttpReceive();
  Future<Channel> fch = channelPool.acquire();
  Channel channel = null;
  try {
    channel = fch.get(timeout, timeUnit);
    ChannelPipeline p = channel.pipeline();
    p.addLast(new HttpClientHandler(httpSend, httpReceive));
    final FullHttpRequest fullHttpRequest = convertRequest(httpSend);
    p.writeAndFlush(fullHttpRequest);
    channel.closeFuture().await(timeout, timeUnit);
    if (!httpReceive.getIsDone()) {
      httpReceive.setHaveError(true);
      httpReceive.setErrMsg("请求已经超时");
    }
  } catch (Exception e) {
    if (log.isWarnEnabled()) {
      log.warn(e.getMessage(), e);
    }
    httpReceive.setHaveError(true)
        .setErrMsg(e.getMessage())
        .setThrowable(e)
        .setIsDone(true);
  } finally {
    if (channel != null) {
      channelPool.release(channel);
    }
  }
  return httpReceive;
}

代码示例来源:origin: org.jboss.xnio.netty/netty-xnio-transport

@Override
public ChannelFuture await() throws InterruptedException {
  inputFuture.await();
  outputFuture.await();
  return this;
}

代码示例来源:origin: org.teiid/teiid-runtime

@Override
public Void get(long arg0, TimeUnit arg1)
    throws InterruptedException, ExecutionException,
    TimeoutException {
  if (future.await(arg0, arg1)) {
    if (!future.isSuccess()) {
      throw new ExecutionException(future.cause());
    }
    return null;
  }
  throw new TimeoutException();
}

代码示例来源:origin: org.zbus/zbus

public int getRealPort(int bindPort) throws InterruptedException{
  if(!serverMap.containsKey(bindPort)){
    return -1; //indicates not found;
  }
  ServerInfo e = serverMap.get(bindPort);
  SocketAddress addr = e.serverChanneFuture.await().channel().localAddress();
  return ((InetSocketAddress)addr).getPort();
}

代码示例来源:origin: com.github.msemys/esjc

private void closeTcpConnection(String reason) {
  if (connection != null) {
    logger.debug("Closing TCP connection, reason: {}", reason);
    try {
      connection.close().await(settings.tcpSettings.closeTimeout.toMillis());
    } catch (Exception e) {
      logger.warn("Unable to close connection gracefully", e);
    }
  } else {
    onTcpConnectionClosed();
  }
}

代码示例来源:origin: lindzh/hasting

@Override
public boolean sendRpcObject(RpcObject rpc, int timeout) {
  ChannelFuture future = channel.writeAndFlush(rpc);
  try {
    future.await(timeout);
    return future.isSuccess();
  } catch (InterruptedException e) {
    return false;
  }
}

代码示例来源:origin: wuyinxian124/nettybook2

public void run(int port) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
  Bootstrap b = new Bootstrap();
  b.group(group).channel(NioDatagramChannel.class)
    .option(ChannelOption.SO_BROADCAST, true)
    .handler(new ChineseProverbServerHandler());
  b.bind(port).sync().channel().closeFuture().await();
} finally {
  group.shutdownGracefully();
}
}

相关文章