好吧,我终于找到办法了:
grpcCleanup.register(InProcessServerBuilder.forName(serverName).intercept(si).directExecutor().addService(service).build().start());
和
private ServerInterceptor si = new ServerInterceptor() {
@Override
public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler next) {
ServerCall wrappedCall = new ForwardingServerCall.SimpleForwardingServerCall(call) {
@Override
public void sendMessage(RespT message) {
super.sendMessage(message);
}
@Override
public void close(Status status, Metadata trailers) {
System.out.println("Interceptor: " + (status.getCause() == null ? "null" : status.getCause().getClass().getName()));
if (status.getCode() == Status.Code.UNKNOWN
&& status.getDescription() == null
&& status.getCause() != null) {
Throwable e = status.getCause();
status = Status.INTERNAL
.withDescription(e.getMessage())
.augmentDescription(stacktraceToString(e));
}
super.close(status, trailers);
}
};
return next.startCall(wrappedCall, headers);
}
private String stacktraceToString(Throwable e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
};