GRPC拦截器如何从消息中提取数据并相互传递?

时间:2018-01-29 19:22:18

标签: java interceptor grpc

我有2个GRPC拦截器:

#1从消息中提取一些数据

#2验证数据并在无效

时关闭调用错误

拦截器#1如何从消息中提取一些数据,将其放入Context并调用拦截器#2(使用带有修改后的Contexts.interceptCall(context, call, headers, next)实例的Context)?

问题是我无法以覆盖<ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next)访问邮件。

使用ForwardingServerCallListener.SimpleForwardingServerCallListener没有帮助,因为拦截器#1的interceptCall()在拦截器#1的onMessage()之前被调用。

private Object msg;
private <ReqT, RespT> ServerCall.Listener<ReqT> interceptCallAfterMessage(
        final ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next) {
      Context context = Context.current().withValue("msg", msg); // error: msg is not yet extracted !
      return Contexts.interceptCall(context, call, headers, next);
}
...
new ForwardingServerCallListener.SimpleForwardingServerCallListener(
    interceptCallAfterMessage(call, headers, next)) { // happens first
    @Override
    public void onMessage(Object message) {
        msg = message; // happens second
        super.onMessage(message);
    }
};

0 个答案:

没有答案