(disclaimer: I'm dabbling in grpc)
I've got an object that I'd like to associate with every request, initializeing it at the start of a request and closeing it once the request is done.
Currently I'm achieving this by using two ServerInterceptors, the first to populate the object into the Context and the second to intercept onReady and onComplete to initialize and close the object respectively.
PopulateObjectServerInterceptor:
public class FooBarInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
FooBar foobar = FooBar.create(foo, bar);
Context ctx = Context.current().withValue(Constants.FOO_BAR, foobar);
return Contexts.interceptCall(ctx, call, headers, next);
}
RequestLifecycleInterceptor
public class FooBarLifecycleServerInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
final ServerCall.Listener<ReqT> original = next.startCall(call, headers);
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(original) {
@Override
public void onReady() {
FooBar foobar = (FooBar) Context.key("foobarname").get();
foobar.initialize()
super.onReady();
}
@Override
public void onMessage(final ReqT message) {
FooBar foo = (FooBar) Context.key("foobar").get();
foobar.someaction()
super.onMessage(message);
}
@Override
public void onComplete() {
FooBar foo = (FooBar) Context.key("foobar").get();
foobar.close()
super.onComplete();
}
}
}
}
Is this the correct/recommended way? I'm wondering whether I can do this with just the one interceptor...