Package com.google.api.gax.rpc
Class ServerStream<V>
- java.lang.Object
-
- com.google.api.gax.rpc.ServerStream<V>
-
- Type Parameters:
V- The type of each response.
- All Implemented Interfaces:
java.lang.Iterable<V>
- Direct Known Subclasses:
BidiStream
public class ServerStream<V> extends java.lang.Object implements java.lang.Iterable<V>A blocking Iterable-style wrapper around server stream responses.This class asynchronously pulls responses from upstream via
StreamController.request(int)and exposes them via its Iterator. The implementation is back pressure aware and uses a constant buffer of 1 item.Please note that the stream can only be consumed once and must either be fully consumed or be canceled.
Neither this class nor the iterator it returns is thread-safe.
Example usage:
ServerStream<Item> stream = ...; for (Item item : stream) { System.out.println(item.id()); // Allow for early termination if (item.id().equals("needle")) { // Cancelling the stream will cause `hasNext()` to return false on the next iteration, // naturally breaking the loop. stream.cancel(); } }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel()Cleanly cancels a partially consumed stream.booleanisReceiveReady()Returns true if the next call to the iterator's hasNext() or next() is guaranteed to be nonblocking.java.util.Iterator<V>iterator()
-
-
-
Method Detail
-
iterator
@Nonnull public java.util.Iterator<V> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<V>
-
isReceiveReady
public boolean isReceiveReady()
Returns true if the next call to the iterator's hasNext() or next() is guaranteed to be nonblocking.- Returns:
- If the call on any of the iterator's methods is guaranteed to be nonblocking.
-
cancel
public void cancel()
Cleanly cancels a partially consumed stream. The associated iterator will return false for the hasNext() in the next iteration. This maintains the contract that an observed true from hasNext() will yield an item in next(), but afterwards will return false.
-
-