@Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } try { call.request(1); @SuppressWarnings("unchecked") T tmp = (T) last; return tmp; } finally { last = null; } }
@Override public boolean hasNext() { if (last == null) { try { // Will block here indefinitely waiting for content. RPC timeouts defend against permanent // hangs here as the call will become closed. last = waitForNext(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw Status.CANCELLED.withDescription("interrupted").withCause(ie).asRuntimeException(); } } if (last instanceof StatusRuntimeException) { // Rethrow the exception with a new stacktrace. StatusRuntimeException e = (StatusRuntimeException) last; throw e.getStatus().asRuntimeException(e.getTrailers()); } return last != this; }
/** * 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; }
/** * 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( ClientCall<ReqT, RespT> call, ReqT param) { BlockingResponseStream<RespT> result = new BlockingResponseStream<RespT>(call); asyncUnaryRequestCall(call, param, result.listener(), true); return result; }