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

x33g5p2x  于2022-01-19 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(182)

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

EventLoop.newFailedFuture介绍

暂无

代码示例

代码示例来源:origin: line/armeria

@Override
public <V> Future<V> newFailedFuture(Throwable cause) {
  return new RequestContextAwareFuture<>(context(), delegate().newFailedFuture(cause));
}

代码示例来源:origin: line/armeria

private void connect(SessionProtocol desiredProtocol, PoolKey key,
           CompletableFuture<PooledChannel> promise) {
  setPendingAcquisition(desiredProtocol, key, promise);
  final InetSocketAddress remoteAddress;
  try {
    remoteAddress = toRemoteAddress(key);
  } catch (UnknownHostException e) {
    notifyConnect(desiredProtocol, key, eventLoop.newFailedFuture(e), promise);
    return;
  }
  // Fail immediately if it is sure that the remote address doesn't support the desired protocol.
  if (SessionProtocolNegotiationCache.isUnsupported(remoteAddress, desiredProtocol)) {
    notifyConnect(desiredProtocol, key,
           eventLoop.newFailedFuture(
               new SessionProtocolNegotiationException(
                   desiredProtocol, "previously failed negotiation")),
           promise);
    return;
  }
  // Create a new connection.
  final Promise<Channel> sessionPromise = eventLoop.newPromise();
  connect(remoteAddress, desiredProtocol, sessionPromise);
  if (sessionPromise.isDone()) {
    notifyConnect(desiredProtocol, key, sessionPromise, promise);
  } else {
    sessionPromise.addListener((Future<Channel> future) -> {
      notifyConnect(desiredProtocol, key, future, promise);
    });
  }
}

代码示例来源:origin: aadnk/ProtocolLib

@Override
public <V> Future<V> newFailedFuture(Throwable arg0) {
  return getDelegate().newFailedFuture(arg0);
}

代码示例来源:origin: CodisLabs/nedis

@Override
public Future<NedisClient> acquire() {
  List<PooledObject> pools = this.pools;
  if (pools.isEmpty()) {
    return poolBuilder.group().next().newFailedFuture(new IOException("Proxy list empty"));
  }
  for (;;) {
    int current = nextIdx.get();
    int next = current >= pools.size() - 1 ? 0 : current + 1;
    if (nextIdx.compareAndSet(current, next)) {
      return pools.get(next).pool.acquire();
    }
  }
}

代码示例来源:origin: CodisLabs/nedis

@Override
public Future<Void> select(int index) {
  if (pool != null) {
    return eventLoop().newFailedFuture(
        new OperationNotSupportedException(
            "'select' is not allowed on a pooled connection"));
  }
  return select0(index);
}

代码示例来源:origin: CodisLabs/nedis

@Override
public Future<Void> auth(byte[] password) {
  if (pool != null) {
    return eventLoop().newFailedFuture(
        new OperationNotSupportedException(
            "'auth' is not allowed on a pooled connection"));
  }
  return auth0(password);
}

代码示例来源:origin: CodisLabs/nedis

@Override
public Future<Void> quit() {
  if (pool != null) {
    return eventLoop().newFailedFuture(
        new OperationNotSupportedException(
            "'quit' is not allowed on a pooled connection"));
  }
  return quit0();
}

代码示例来源:origin: CodisLabs/nedis

@Override
public Future<Void> clientSetname(byte[] name) {
  if (pool != null) {
    return eventLoop().newFailedFuture(
        new OperationNotSupportedException(
            "'client setname' is not allowed on a pooled connection"));
  }
  return clientSetname0(name);
}

代码示例来源:origin: com.simplyti.cloud/simple-server-clients

public Future<Void> send(T msg) {
  if (clientChannel.isDone()) {
    if (clientChannel.isSuccess()) {
      if(pendingDone) {
        Promise<Void> promise = executor.newPromise();
        clientChannel.getNow().writeAndFlush(msg).addListener(f->toPromise(f,promise));
        return promise;
      }else {
        return addPending(msg);
      }
    }else {
      ReferenceCountUtil.release(msg);
      return executor.newFailedFuture(clientChannel.cause());
    }
  }else {
    return addPending(msg);
  }
}

代码示例来源:origin: airlift/drift

@Override
public Future<Channel> getConnection(ConnectionParameters connectionParameters, HostAndPort address)
{
  ConnectionKey key = new ConnectionKey(connectionParameters, address);
  while (true) {
    synchronized (this) {
      if (closed) {
        return group.next().newFailedFuture(new TTransportException("Connection pool is closed"));
      }
      Future<Channel> future;
      try {
        future = cachedConnections.get(key, () -> createConnection(key));
      }
      catch (ExecutionException e) {
        throw new RuntimeException(e);
      }
      // connection is still opening
      if (!future.isDone()) {
        return future;
      }
      // check if connection is failed or closed
      if (future.getNow().isOpen()) {
        return future;
      }
      // remove dead connection from cache
      cachedConnections.asMap().remove(key, future);
    }
  }
}

代码示例来源:origin: io.airlift.drift/drift-transport-netty

@Override
public Future<Channel> getConnection(ConnectionParameters connectionParameters, HostAndPort address)
{
  ConnectionKey key = new ConnectionKey(connectionParameters, address);
  while (true) {
    synchronized (this) {
      if (closed) {
        return group.next().newFailedFuture(new TTransportException("Connection pool is closed"));
      }
      Future<Channel> future;
      try {
        future = cachedConnections.get(key, () -> createConnection(key));
      }
      catch (ExecutionException e) {
        throw new RuntimeException(e);
      }
      // connection is still opening
      if (!future.isDone()) {
        return future;
      }
      // check if connection is failed or closed
      if (future.getNow().isOpen()) {
        return future;
      }
      // remove dead connection from cache
      cachedConnections.asMap().remove(key, future);
    }
  }
}

代码示例来源:origin: com.microsoft.rest.v2/client-runtime

@Override
public Future<Void> release(final Channel channel) {
  try {
    handler.channelReleased(channel);
    synchronized (sync) {
      leased.remove(channel.attr(CHANNEL_URI).get(), channel);
      if (isChannelHealthy(channel)) {
        available.put(channel.attr(CHANNEL_URI).get(), channel);
        channel.attr(CHANNEL_AVAILABLE_SINCE).set(ZonedDateTime.now(ZoneOffset.UTC));
        logger.debug("Channel released to pool: " + channel.id());
      } else {
        channelCount.decrementAndGet();
        logger.debug("Channel broken on release, dispose: " + channel.id());
      }
      sync.notify();
    }
  } catch (Exception e) {
    return bootstrap.config().group().next().newFailedFuture(e);
  }
  return bootstrap.config().group().next().newSucceededFuture(null);
}

代码示例来源:origin: CodisLabs/nedis

public Future<RoundRobinNedisClientPool> build() {
    validate();
    try {
      return new RoundRobinNedisClientPool(curatorClient, closeCurator, zkProxyDir,
          poolBuilder).initFuture();
    } catch (Exception e) {
      return poolBuilder.group().next().newFailedFuture(e);
    }
  }
}

代码示例来源:origin: io.airlift.drift/drift-transport-netty

@Override
public Future<Channel> getConnection(ConnectionParameters connectionParameters, HostAndPort address)
{
  try {
    Bootstrap bootstrap = new Bootstrap()
        .group(group)
        .channel(NioSocketChannel.class)
        .option(ALLOCATOR, allocator)
        .option(CONNECT_TIMEOUT_MILLIS, saturatedCast(connectionParameters.getConnectTimeout().toMillis()))
        .handler(new ThriftClientInitializer(
            connectionParameters.getTransport(),
            connectionParameters.getProtocol(),
            connectionParameters.getMaxFrameSize(),
            connectionParameters.getRequestTimeout(),
            connectionParameters.getSocksProxy(),
            connectionParameters.getSslContextParameters().map(sslContextFactory::get)));
    Promise<Channel> promise = group.next().newPromise();
    bootstrap.connect(new InetSocketAddress(address.getHost(), address.getPort()))
        .addListener((ChannelFutureListener) future -> notifyConnect(future, promise));
    return promise;
  }
  catch (Throwable e) {
    return group.next().newFailedFuture(new TTransportException(e));
  }
}

代码示例来源:origin: airlift/drift

@Override
public Future<Channel> getConnection(ConnectionParameters connectionParameters, HostAndPort address)
{
  try {
    Bootstrap bootstrap = new Bootstrap()
        .group(group)
        .channel(NioSocketChannel.class)
        .option(ALLOCATOR, allocator)
        .option(CONNECT_TIMEOUT_MILLIS, saturatedCast(connectionParameters.getConnectTimeout().toMillis()))
        .handler(new ThriftClientInitializer(
            connectionParameters.getTransport(),
            connectionParameters.getProtocol(),
            connectionParameters.getMaxFrameSize(),
            connectionParameters.getRequestTimeout(),
            connectionParameters.getSocksProxy(),
            connectionParameters.getSslContextParameters().map(sslContextFactory::get)));
    Promise<Channel> promise = group.next().newPromise();
    bootstrap.connect(new InetSocketAddress(address.getHost(), address.getPort()))
        .addListener((ChannelFutureListener) future -> notifyConnect(future, promise));
    return promise;
  }
  catch (Throwable e) {
    return group.next().newFailedFuture(new TTransportException(e));
  }
}

代码示例来源:origin: com.simplyti.cloud/simple-server-clients

public <T> Future<ClientRequestChannel<T>> channel(ClientConfig config, ClientRequestChannelInitializer<T> clientRequestChannelHandler, Promise<T> resultPromise) {
  ChannelPool pool = channelPoolMap.get(config.endpoint());
  Future<Channel> channelFuture = pool.acquire();
  if (channelFuture.isDone()) {
    if (channelFuture.isSuccess()) {
      Channel channel = channelFuture.getNow();
      return clientRequestChannel(config,clientRequestChannelHandler, pool,channel,resultPromise);
    } else {
      return eventLoopGroup.next().newFailedFuture(channelFuture.cause());
    }
  } else {
    EventLoop eventLoop = eventLoopGroup.next();
    Promise<ClientRequestChannel<T>> clientPromise = eventLoop.newPromise();
    channelFuture.addListener(f -> {
      if (channelFuture.isSuccess()) {
        Future<ClientRequestChannel<T>> fcrc = clientRequestChannel(config,clientRequestChannelHandler, pool,channelFuture.getNow(),resultPromise);
        Promises.toPromise(fcrc,clientPromise);
      } else {
        clientPromise.setFailure(channelFuture.cause());
      }
    });
    return clientPromise;
  } 
}

代码示例来源:origin: CodisLabs/nedis

@Override
public Future<NedisClient> acquire() {
  synchronized (pool) {
    if (closed) {
      return group.next().<NedisClient>newFailedFuture(
          new IllegalStateException("already closed"));
    }
    if (numConns < maxPooledConns) {
      numConns++;
      return newClient().addListener(acquireFutureListener);
    }
    if (!pool.isEmpty()) {
      NedisClient client = pool.head(exclusive);
      return client.eventLoop().newSucceededFuture(client);
    }
    if (exclusive) {
      numConns++;
      return newClient().addListener(acquireFutureListener);
    } else {
      // If connection is shared, then we should not create more connections than
      // maxPooledConns. So here we add a promise to pending queue. The promise will be
      // notified when there are connections in pool.
      Promise<NedisClient> promise = group.next().newPromise();
      pendingAcquireList.add(promise);
      return promise;
    }
  }
}

相关文章