A blocking Iterable-style wrapper around server stream responses.
This class asynchronously pulls responses from upstream viaStreamController#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(Itemitem:stream){System.out.println(item.id());// Allow for early terminationif(item.id().equals("needle")){// Cancelling the stream will causehasNext()to return false on the next iteration,// naturally breaking the loop.stream.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.
isReceiveReady()
publicbooleanisReceiveReady()
Returns true if the next call to the iterator's hasNext() or next() is guaranteed to be
nonblocking.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis webpage provides documentation for the \u003ccode\u003eServerStream<V>\u003c/code\u003e class in the Google API Client Library for Java (gax), covering versions from 2.7.1 up to the latest version 2.63.1.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eServerStream<V>\u003c/code\u003e is a class that offers a blocking, iterable interface for managing server stream responses, enabling asynchronous retrieval of data.\u003c/p\u003e\n"],["\u003cp\u003eThe class allows for clean cancellation of a partially consumed stream via the \u003ccode\u003ecancel()\u003c/code\u003e method, ensuring that subsequent calls to \u003ccode\u003ehasNext()\u003c/code\u003e on the iterator return \u003ccode\u003efalse\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eServerStream\u003c/code\u003e class is not thread-safe, and the stream can only be consumed once, either fully or by being cancelled.\u003c/p\u003e\n"],["\u003cp\u003eIt implements the Iterable interface and provides methods such as \u003ccode\u003eisReceiveReady()\u003c/code\u003e, \u003ccode\u003eiterator()\u003c/code\u003e, and \u003ccode\u003estream()\u003c/code\u003e to manage and access the server responses.\u003c/p\u003e\n"]]],[],null,["# Class ServerStream<V> (2.69.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.69.0 (latest)](/java/docs/reference/gax/latest/com.google.api.gax.rpc.ServerStream)\n- [2.68.2](/java/docs/reference/gax/2.68.2/com.google.api.gax.rpc.ServerStream)\n- [2.67.0](/java/docs/reference/gax/2.67.0/com.google.api.gax.rpc.ServerStream)\n- [2.66.0](/java/docs/reference/gax/2.66.0/com.google.api.gax.rpc.ServerStream)\n- [2.65.0](/java/docs/reference/gax/2.65.0/com.google.api.gax.rpc.ServerStream)\n- [2.63.1](/java/docs/reference/gax/2.63.1/com.google.api.gax.rpc.ServerStream)\n- [2.62.0](/java/docs/reference/gax/2.62.0/com.google.api.gax.rpc.ServerStream)\n- [2.61.0](/java/docs/reference/gax/2.61.0/com.google.api.gax.rpc.ServerStream)\n- [2.60.0](/java/docs/reference/gax/2.60.0/com.google.api.gax.rpc.ServerStream)\n- [2.59.1](/java/docs/reference/gax/2.59.1/com.google.api.gax.rpc.ServerStream)\n- [2.58.0](/java/docs/reference/gax/2.58.0/com.google.api.gax.rpc.ServerStream)\n- [2.57.0](/java/docs/reference/gax/2.57.0/com.google.api.gax.rpc.ServerStream)\n- [2.55.0](/java/docs/reference/gax/2.55.0/com.google.api.gax.rpc.ServerStream)\n- [2.54.1](/java/docs/reference/gax/2.54.1/com.google.api.gax.rpc.ServerStream)\n- [2.53.0](/java/docs/reference/gax/2.53.0/com.google.api.gax.rpc.ServerStream)\n- [2.52.0](/java/docs/reference/gax/2.52.0/com.google.api.gax.rpc.ServerStream)\n- [2.51.0](/java/docs/reference/gax/2.51.0/com.google.api.gax.rpc.ServerStream)\n- [2.50.0](/java/docs/reference/gax/2.50.0/com.google.api.gax.rpc.ServerStream)\n- [2.49.0](/java/docs/reference/gax/2.49.0/com.google.api.gax.rpc.ServerStream)\n- [2.48.1](/java/docs/reference/gax/2.48.1/com.google.api.gax.rpc.ServerStream)\n- [2.47.0](/java/docs/reference/gax/2.47.0/com.google.api.gax.rpc.ServerStream)\n- [2.46.1](/java/docs/reference/gax/2.46.1/com.google.api.gax.rpc.ServerStream)\n- [2.45.0](/java/docs/reference/gax/2.45.0/com.google.api.gax.rpc.ServerStream)\n- [2.43.0](/java/docs/reference/gax/2.43.0/com.google.api.gax.rpc.ServerStream)\n- [2.42.0](/java/docs/reference/gax/2.42.0/com.google.api.gax.rpc.ServerStream)\n- [2.41.0](/java/docs/reference/gax/2.41.0/com.google.api.gax.rpc.ServerStream)\n- [2.39.0](/java/docs/reference/gax/2.39.0/com.google.api.gax.rpc.ServerStream)\n- [2.38.0](/java/docs/reference/gax/2.38.0/com.google.api.gax.rpc.ServerStream)\n- [2.37.0](/java/docs/reference/gax/2.37.0/com.google.api.gax.rpc.ServerStream)\n- [2.36.0](/java/docs/reference/gax/2.36.0/com.google.api.gax.rpc.ServerStream)\n- [2.35.0](/java/docs/reference/gax/2.35.0/com.google.api.gax.rpc.ServerStream)\n- [2.34.1](/java/docs/reference/gax/2.34.1/com.google.api.gax.rpc.ServerStream)\n- [2.33.0](/java/docs/reference/gax/2.33.0/com.google.api.gax.rpc.ServerStream)\n- [2.32.1](/java/docs/reference/gax/2.32.1/com.google.api.gax.rpc.ServerStream)\n- [2.31.1](/java/docs/reference/gax/2.31.1/com.google.api.gax.rpc.ServerStream)\n- [2.30.1](/java/docs/reference/gax/2.30.1/com.google.api.gax.rpc.ServerStream)\n- [2.24.0](/java/docs/reference/gax/2.24.0/com.google.api.gax.rpc.ServerStream)\n- [2.23.3](/java/docs/reference/gax/2.23.3/com.google.api.gax.rpc.ServerStream)\n- [2.22.0](/java/docs/reference/gax/2.22.0/com.google.api.gax.rpc.ServerStream)\n- [2.21.0](/java/docs/reference/gax/2.21.0/com.google.api.gax.rpc.ServerStream)\n- [2.20.1](/java/docs/reference/gax/2.20.1/com.google.api.gax.rpc.ServerStream)\n- [2.19.6](/java/docs/reference/gax/2.19.6/com.google.api.gax.rpc.ServerStream)\n- [2.18.7](/java/docs/reference/gax/2.18.7/com.google.api.gax.rpc.ServerStream)\n- [2.17.0](/java/docs/reference/gax/2.17.0/com.google.api.gax.rpc.ServerStream)\n- [2.16.0](/java/docs/reference/gax/2.16.0/com.google.api.gax.rpc.ServerStream)\n- [2.15.0](/java/docs/reference/gax/2.15.0/com.google.api.gax.rpc.ServerStream)\n- [2.14.0](/java/docs/reference/gax/2.14.0/com.google.api.gax.rpc.ServerStream)\n- [2.13.0](/java/docs/reference/gax/2.13.0/com.google.api.gax.rpc.ServerStream)\n- [2.12.2](/java/docs/reference/gax/2.12.2/com.google.api.gax.rpc.ServerStream)\n- [2.11.0](/java/docs/reference/gax/2.11.0/com.google.api.gax.rpc.ServerStream)\n- [2.10.0](/java/docs/reference/gax/2.10.0/com.google.api.gax.rpc.ServerStream)\n- [2.9.0](/java/docs/reference/gax/2.9.0/com.google.api.gax.rpc.ServerStream)\n- [2.8.1](/java/docs/reference/gax/2.8.1/com.google.api.gax.rpc.ServerStream)\n- [2.7.1](/java/docs/reference/gax/2.7.1/com.google.api.gax.rpc.ServerStream) \n\n public class ServerStream\u003cV\u003e implements Iterable\u003cV\u003e\n\nA blocking Iterable-style wrapper around server stream responses.\n\nThis class asynchronously pulls responses from upstream via [StreamController#request(int)](/java/docs/reference/gax/latest/com.google.api.gax.rpc.StreamController#com_google_api_gax_rpc_StreamController_request_int_) and exposes them via its Iterator. The implementation is back\npressure aware and uses a constant buffer of 1 item.\n\nPlease note that the stream can only be consumed once and must either be fully consumed or be\ncanceled.\n\nNeither this class nor the iterator it returns is thread-safe.\n\nExample usage: \n\n\n ServerStream\u003cItem\u003e stream = ...;\n\n for (Item item : stream) {\n System.out.println(item.id());\n\n // Allow for early termination\n if (item.id().equals(\"needle\")) {\n // Cancelling the stream will cause `hasNext()` to return false on the next iteration,\n // naturally breaking the loop.\n stream.cancel();\n }\n }\n \nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e ServerStream\\\u003cV\\\u003e \n\nImplements\n----------\n\n[Iterable\\\u003cV\\\u003e](https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable\u003cV\u003e.html) \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nMethods\n-------\n\n### cancel()\n\n public void cancel()\n\nCleanly cancels a partially consumed stream. The associated iterator will return false for the\nhasNext() in the next iteration. This maintains the contract that an observed true from\nhasNext() will yield an item in next(), but afterwards will return false.\n\n### isReceiveReady()\n\n public boolean isReceiveReady()\n\nReturns true if the next call to the iterator's hasNext() or next() is guaranteed to be\nnonblocking.\n\n### iterator()\n\n public Iterator\u003cV\u003e iterator()\n\n### stream()\n\n public Stream\u003cV\u003e stream()\n\nReturns a sequential `Stream` with server responses as its source."]]