Java pattern for response observer that requires access to request observer?

26 views
Skip to first unread message

luis.e....@gmail.com

unread,
Dec 6, 2017, 11:09:50 PM12/6/17
to grpc.io
I have a bi-directional RPC, let's use the sample StreamingGreeter.

I need to register a response observer:

// a response observer
StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() { ... };


StreamingGreeterGrpc.StreamingGreeterStub stub = StreamingGreeterGrpc.newStub(channel);



// calling the streaming rpc returns the request observer
StreamObserver<HelloRequest> requestObserver = stub.SayHelloStreaming(responseObserver);



So, how do I safely use the request observer from within the response observer, given that the response observer has to be created before the rpc call and the request observer is only available after the rpc call?

In my example, upon receiving HelloReply, I might want to, for example trigger sending another HelloRequest (for whatever reason).



William Thurston

unread,
Dec 6, 2017, 11:28:16 PM12/6/17
to luis.e....@gmail.com, grpc.io
In the past, I created a final AtomicReference and assigned the requestObserver to it and then called a get on the atomic reference in the onNext of the response onserver when I wanted to ping pong back and forth. It works, but also felt a bit off, so if anyone has better I’d love to hear it!

William Thurston
--
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 grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
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/2a232c67-14d9-4700-9b35-47cababb7c22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages