Close grpc stream iterator response from client side in Java

35 views
Skip to first unread message

rafael...@gmail.com

unread,
Jan 31, 2019, 7:51:27 AM1/31/19
to grpc.io

Hi all,

I am facing some problems trying to close a Server-Stream call from the client side.

The client opens the channel, create the stub, etc and try to perform the call which returns an Iterator<Item>.

The grpc java examples show how to read the data from this Iterator is using for example a while loop, but not how the client could close it (onle the server can do so)

<code>

Iterator<Item> response = stub.getData()

while(response.hasNext())
{
      response.next();
}

</code>

In case I want to close this call, what is the best way to do it? or at least something that makes it works?

I have tried out something like this, whenever it goes out of the while loop it will "destroy, cancel" the iterator, but it does not work:

<code>

Iterator<Item> response = stub.getData()

while(response.hasNext() && !condition)
{
      response.next();
}
response = null;

</code>

Kind regards,
Rafael.

dan...@wso2.com

unread,
Jan 31, 2019, 9:29:52 AM1/31/19
to grpc.io
Hi Rafael,

In gRPC server streaming, client doesn't need to close the connection. When client sends the server request, client half closes the connection and listen to server response. 

According to the example code, looks like you are making blocking server streaming call from client.

In that case, when you perform the call and you get response, server already sent all responses to client and both client and server close the connection. We can read server responses from iterator.

Please correct me if I am wrong.

Thanks
Danesh
Reply all
Reply to author
Forward
0 new messages