private Object waitForNext() throws InterruptedException { if (threadless == null) { return buffer.take(); } else { Object next = buffer.poll(); while (next == null) { threadless.waitAndDrain(); next = buffer.poll(); } return next; } }
/** * Executes a unary call and blocks on the response. * * @return the single response message. */ public static <ReqT, RespT> RespT blockingUnaryCall( Channel channel, MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, ReqT param) { ThreadlessExecutor executor = new ThreadlessExecutor(); ClientCall<ReqT, RespT> call = channel.newCall(method, callOptions.withExecutor(executor)); try { ListenableFuture<RespT> responseFuture = futureUnaryCall(call, param); while (!responseFuture.isDone()) { try { executor.waitAndDrain(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Status.CANCELLED .withDescription("Call was interrupted") .withCause(e) .asRuntimeException(); } } return getUnchecked(responseFuture); } catch (RuntimeException e) { throw cancelThrow(call, e); } catch (Error e) { throw cancelThrow(call, e); } }
/** * Executes a server-streaming call returning a blocking {@link Iterator} over the * response stream. * * @return an iterator over the response stream. */ // TODO(louiscryan): Not clear if we want to use this idiom for 'simple' stubs. public static <ReqT, RespT> Iterator<RespT> blockingServerStreamingCall( Channel channel, MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, ReqT param) { ThreadlessExecutor executor = new ThreadlessExecutor(); ClientCall<ReqT, RespT> call = channel.newCall(method, callOptions.withExecutor(executor)); BlockingResponseStream<RespT> result = new BlockingResponseStream<RespT>(call, executor); asyncUnaryRequestCall(call, param, result.listener(), true); return result; }