List of usage examples for io.netty.util.concurrent Promise isSuccess
boolean isSuccess();
From source file:com.linecorp.armeria.client.HttpRemoteInvoker.java
License:Apache License
static <T> void invoke0(ClientCodec codec, Channel channel, Method method, Object[] args, ClientOptions options, Promise<T> resultPromise, PoolKey poolKey) { final HttpSession session = HttpSessionHandler.get(channel); final SessionProtocol sessionProtocol = session.protocol(); if (sessionProtocol == null) { resultPromise.setFailure(ClosedSessionException.INSTANCE); return;/*from ww w . j a v a 2s . co m*/ } final EncodeResult encodeResult = codec.encodeRequest(channel, sessionProtocol, method, args); if (encodeResult.isSuccess()) { ServiceInvocationContext ctx = encodeResult.invocationContext(); Promise<FullHttpResponse> responsePromise = channel.eventLoop().newPromise(); final Invocation invocation = new Invocation(ctx, options, responsePromise, encodeResult.content()); //write request final ChannelFuture writeFuture = writeRequest(channel, invocation, ctx, options); writeFuture.addListener(fut -> { if (!fut.isSuccess()) { ctx.rejectPromise(responsePromise, fut.cause()); } else { long responseTimeoutMillis = options.responseTimeoutPolicy().timeout(ctx); scheduleTimeout(channel, responsePromise, responseTimeoutMillis, false); } }); //handle response if (responsePromise.isSuccess()) { decodeResult(codec, resultPromise, ctx, responsePromise.getNow()); } else { responsePromise.addListener((Future<FullHttpResponse> future) -> { if (future.isSuccess()) { decodeResult(codec, resultPromise, ctx, responsePromise.getNow()); } else { ctx.rejectPromise(resultPromise, future.cause()); } }); } } else { final Throwable cause = encodeResult.cause(); if (!resultPromise.tryFailure(cause)) { logger.warn("Failed to reject an invocation promise ({}) with {}", resultPromise, cause, cause); } } if (!session.onRequestSent()) { // Can't send a request via the current session anymore; do not return the channel to the pool. return; } // Return the channel to the pool. final KeyedChannelPool<PoolKey> pool = KeyedChannelPool.findPool(channel); if (sessionProtocol.isMultiplex()) { pool.release(poolKey, channel); } else { resultPromise.addListener(fut -> pool.release(poolKey, channel)); } }
From source file:me.ferrybig.javacoding.teamspeakconnector.internal.TeamspeakIO.java
License:Open Source License
public Future<ComplexResponse> sendPacket(ComplexRequest req, SendBehaviour sendBehaviour) { if (closed) { if (sendBehaviour != SendBehaviour.NORMAL) { return this.closeFuture; }// w ww .j ava 2 s . co m return channel.eventLoop().newFailedFuture(new TeamspeakException("Channel closed")); } Promise<ComplexResponse> prom = channel.eventLoop().newPromise(); ChannelFuture future; synchronized (incomingQueue) { if (closed) { if (sendBehaviour != SendBehaviour.NORMAL) { return this.closeFuture; } return prom.setFailure(new TeamspeakException("Channel closed")); } incomingQueue.offer(new PendingPacket(prom, req, sendBehaviour)); future = channel.writeAndFlush(req); } future.addListener(upstream -> { assert upstream == future; if (sendBehaviour == SendBehaviour.FORCE_CLOSE_CONNECTION) { channel.eventLoop().schedule(() -> { if (channel.isActive()) { LOG.fine("Closing channel by timeout"); channel.close(); } }, 10, TimeUnit.SECONDS); } if (!upstream.isSuccess()) { synchronized (incomingQueue) { if (incomingQueue.removeIf(prom::equals)) { prom.setFailure(new TeamspeakException("Exception during sending", upstream.cause())); } } } }); if (sendBehaviour == SendBehaviour.CLOSE_CONNECTION || sendBehaviour == SendBehaviour.FORCE_CLOSE_CONNECTION) { prom.addListener(upstream -> { assert upstream == prom; if (prom.isSuccess()) { synchronized (incomingQueue) { this.closed = true; } channel.close(); LOG.fine("Closing channel because sendmessage asked it"); } }); } return prom; }
From source file:org.apache.hive.spark.client.rpc.RpcServer.java
License:Apache License
@VisibleForTesting Future<Rpc> registerClient(final String clientId, String secret, RpcDispatcher serverDispatcher, long clientTimeoutMs) { final Promise<Rpc> promise = group.next().newPromise(); Runnable timeout = new Runnable() { @Override/*from ww w . j a v a 2s. c o m*/ public void run() { promise.setFailure(new TimeoutException("Timed out waiting for client connection.")); } }; ScheduledFuture<?> timeoutFuture = group.schedule(timeout, clientTimeoutMs, TimeUnit.MILLISECONDS); final ClientInfo client = new ClientInfo(clientId, promise, secret, serverDispatcher, timeoutFuture); if (pendingClients.putIfAbsent(clientId, client) != null) { throw new IllegalStateException(String.format("Client '%s' already registered.", clientId)); } promise.addListener(new GenericFutureListener<Promise<Rpc>>() { @Override public void operationComplete(Promise<Rpc> p) { if (!p.isSuccess()) { pendingClients.remove(clientId); } } }); return promise; }