Help with Java gRPC server coding

84 views
Skip to first unread message

wbvre...@gmail.com

unread,
Jun 29, 2017, 11:04:42 AM6/29/17
to grpc.io
Hi All,

I'm a newbie with regards to gRPC and have been reading and searching a lot the last couple of days, but I can't seem to find the right answer. I have a service definition in my proto that looks like this:

   /// Requests a function to be executed as specified in the header.
    rpc ExecuteFunction (stream BundledRows) returns (stream BundledRows) {}

So in the generated code I get:

public io.grpc.stub.StreamObserver<net.reeuwijk.qlik.aai.regexp.BundledRows> executeFunction(
       io.grpc.stub.StreamObserver<net.reeuwijk.qlik.aai.regexp.BundledRows> responseObserver) {

Which is expected. However in the code of this method I need access to header information. In the Python implementation I can get this from the context. I have looked at adding an HeaderInterceptor, but this looks way to complicated :-)

So my question. In order to get the metadata out of the headers do I need to use the HeaderInterceptor? or is there another way?

Any hints, tips or trips are highly appreciated.

Regards,
Bas van Reeuwijk

Eric Anderson

unread,
Jun 29, 2017, 2:09:41 PM6/29/17
to wbvre...@gmail.com, grpc.io
You need to use a ServerInterceptor. Typically the interceptor will do any necessary decoding, and then put the result in a io.grpc.Context (using also io.grpc.Contexts.interceptorCall) for the handler to access.

Consider the case of Authentication: the interceptor would verify the user's identity and then put the verified identity on the Context. This greatly reduces the chances that a method may fail to verify the user's identity and instead just blindly trusted the client to be who they said they were.

--
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+unsubscribe@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/41222d23-9b8e-4fad-a068-d0ce0cfd6484%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

wbvre...@gmail.com

unread,
Jul 6, 2017, 4:56:33 AM7/6/17
to grpc.io, wbvre...@gmail.com
Thanks Eric,

I have defined the ServerInterceptor and implemented the interceptCall method. I'm able to get the information from the requestHeaders and I add them to the current Context using:
    ScriptRequestHeader scriptRequestHeader = ScriptRequestHeader.parseFrom(h);
    Context.current().withValue(SCRIPT_HEADER, scriptRequestHeader);

My remaining question is, how do I access this Context inside the handler code? What is the scope of the Context?
Sorry for the ignorant questions, but it's my first foray into gRPC.

Regards,
Bas. 

Op donderdag 29 juni 2017 20:09:41 UTC+2 schreef Eric Anderson:
You need to use a ServerInterceptor. Typically the interceptor will do any necessary decoding, and then put the result in a io.grpc.Context (using also io.grpc.Contexts.interceptorCall) for the handler to access.

Consider the case of Authentication: the interceptor would verify the user's identity and then put the verified identity on the Context. This greatly reduces the chances that a method may fail to verify the user's identity and instead just blindly trusted the client to be who they said they were.
On Thu, Jun 29, 2017 at 8:04 AM, <wbvre...@gmail.com> wrote:
Hi All,

I'm a newbie with regards to gRPC and have been reading and searching a lot the last couple of days, but I can't seem to find the right answer. I have a service definition in my proto that looks like this:

   /// Requests a function to be executed as specified in the header.
    rpc ExecuteFunction (stream BundledRows) returns (stream BundledRows) {}

So in the generated code I get:

public io.grpc.stub.StreamObserver<net.reeuwijk.qlik.aai.regexp.BundledRows> executeFunction(
       io.grpc.stub.StreamObserver<net.reeuwijk.qlik.aai.regexp.BundledRows> responseObserver) {

Which is expected. However in the code of this method I need access to header information. In the Python implementation I can get this from the context. I have looked at adding an HeaderInterceptor, but this looks way to complicated :-)

So my question. In order to get the metadata out of the headers do I need to use the HeaderInterceptor? or is there another way?

Any hints, tips or trips are highly appreciated.

Regards,
Bas van Reeuwijk

--
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.

Eric Anderson

unread,
Jul 7, 2017, 11:35:34 AM7/7/17
to Bas van Reeuwijk, grpc.io
Context.current().withValue() creates a new Context with the modification. You need to attach it to the current thread for it to be seen by your application (so that Context.current() returns your new Context). For an interceptor, using io.grpc.Contexts.interceptorCall will do that for you.

Then, from your application call SCRIPT_HEADER.get(). This uses Context.current() implicitly.

The current context is stored on a ThreadLocal, so if you do processing on another thread and need access to the Context, you may need to propagate the Context. Context has some convenience methods to help here. For example, wrapping an executor via Context.currentContextExecutor(Executor) will cause the Context to be propagated when Executor.execute(Runnable) is called. You could make sure that any Executor you pass to your application is Context-aware and then you shouldn't need to bother manually copying the context.

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
Reply all
Reply to author
Forward
0 new messages