H Ryan, Thank you for your prompt response. I have a doubt about the example you have cited. I believe that the "beforeStart" method needs to be call and needs a request stream. In my case I don't have a request stream as it's a server side streaming. Pls. do let me know if there any alternate ways to achieve our use case.
Thanks, Suvhrajit Basak On Wed, Oct 18, 2017 at 3:28 AM, Ryan Michela <[email protected]> wrote: > > Something like this might work* (untested)*: > > public abstract class CancelableClientCallStreamObserver<TResp> > extends ClientCallStreamObserver<TResp> > implements ClientResponseObserver<Object, TResp> { > > > private ClientCallStreamObserver requestStream; > > > @Override > public void cancel(@Nullable String message, @Nullable Throwable cause > ) { > if (requestStream != null) { > requestStream.cancel(message, cause); > } > } > > > @Override > public void beforeStart(ClientCallStreamObserver requestStream) { > this.requestStream = requestStream; > } > } > > You would use it like this: > > CancelableClientCallStreamObserver<ResponseType> responseObserver = new > CancelableClientCallStreamObserver<>() { > @Override > public void onNext(TResp value) { > ... > } > > @Override > public void onError(Throwable t) { > ... > } > > @Override > public void onCompleted() { > ... > } > } > > nonblockingStub.listFeatures(request, responseObserver); > > Thread.sleep(15000); > *responseObserver**.cancel();* > > > On Tuesday, October 17, 2017 at 2:27:32 AM UTC-7, [email protected] wrote: >> >> Hello, >> >> I have a use case to have a stream continuously opened between server >> and the client. The server uses the stream to push data to the client. >> However, we have certain scenarios wherein we might need to terminate the >> stream. I tried using the following wherein I could find that the client >> gets closed but the server still continues to push the data. >> >> CancellableContext withCancellation = Context.current().withCancella >> tion(); >> withCancellation.run(new Runnable() { >> @Override >> public void run() { >> nonblockingStub.listFeatures(request, responseObserver); >> } >> }); >> >> Thread.sleep(15000); >> withCancellation.cancel(null); >> Thread.sleep(15000); >> >> Also would like to know if there is an option for an idle timeout for a >> stream wherein if there are no traffic on the stream for a specified >> duration the stream gets terminated something similar to keepAlive for >> ManagedChannelBuilder. >> >> Note: We are having a server side streaming only. >> >> >> Waiting eagerly for your replies. :) >> >> Regards >> Suv >> >> >> -- > You received this message because you are subscribed to a topic in the > Google Groups "grpc.io" group. > To unsubscribe from this topic, visit https://groups.google.com/d/ > topic/grpc-io/mC9Vm3fRnNU/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/grpc-io. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/grpc-io/413af003-5fd8-49f8-b779-b0bbb5951ab7%40googlegroups.com > <https://groups.google.com/d/msgid/grpc-io/413af003-5fd8-49f8-b779-b0bbb5951ab7%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "grpc.io" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/grpc-io. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/CACd0cy6Bjnw1YkwCvfmNq_05BY6tS2tUzSbB28dwYrtyqRUh1Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
