public class GrpcExceptionHandler implements ServerInterceptor {
private final Logger logger = LoggerFactory.getLogger (GrpcExceptionHandler.class);
@Override
public ServerCall.Listener interceptCall (ServerCall call,
Metadata headers,
ServerCallHandler next) {
logger.info ("GRPC call at: {}", Instant.now());
ServerCall.Listener listener;
try {
listener = next.startCall (call, headers);
} catch (Throwable ex) {
logger.error ("Uncaught exception from grpc service");
call.close (Status.INTERNAL
.withCause (ex)
.withDescription ("Uncaught exception from grpc service"), null);
return new ServerCall.Listener() {};
}
return listener;
}
}
样品上面的拦截。
你需要引导它,当然,在期待任何东西之前;
serverBuilder.addService (ServerInterceptors.intercept (bindableService, interceptor));
UPDATE
public interface ServerCallHandler {
/**
* Produce a non-{@code null} listener for the incoming call. Implementations are free to call
* methods on {@code call} before this method has returned.
*
*
If the implementation throws an exception, {@code call} will be closed with an error.
* Implementations must not throw an exception if they started processing that may use {@code
* call} on another thread.
*
* @param call object for responding to the remote client.
* @return listener for processing incoming request messages for {@code call}
*/
ServerCall.Listener startCall(
ServerCall call,
Metadata headers);
}
可悲的是,不同的线程上下文意味着没有异常处理的范围,所以我的答案是不是你正在寻找解决方案..