首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grpc java中的低级事件

grpc java中的低级事件
EN

Stack Overflow用户
提问于 2021-11-17 02:46:51
回答 1查看 26关注 0票数 0

用例是,我们有一个单独的流和通道,我们通过它发送具有非常大的有效负载的消息。同时,我们也有一个应用程序级别的ping,如果我们在最后10秒内没有看到新的消息,它就会超时。在速度较慢的网络上,从流中读取数据需要花费大量时间。

当完整的消息被读取和反序列化时,我们从GRPC得到的唯一回调是onNext。有没有办法获取较低级别的事件,比如正在进行的流读取?或者是否有一种方法可以拦截流阅读器

代码语言:javascript
复制
public interface StreamObserver<V>  {
  /**
   * Receives a value from the stream.
   *
   * <p>Can be called many times but is never called after {@link #onError(Throwable)} or {@link
   * #onCompleted()} are called.
   *
   * <p>Unary calls must invoke onNext at most once.  Clients may invoke onNext at most once for
   * server streaming calls, but may receive many onNext callbacks.  Servers may invoke onNext at
   * most once for client streaming calls, but may receive many onNext callbacks.
   *
   * <p>If an exception is thrown by an implementation the caller is expected to terminate the
   * stream by calling {@link #onError(Throwable)} with the caught exception prior to
   * propagating it.
   *
   * @param value the value passed to the stream
   */
  void onNext(V value);

  /**
   * Receives a terminating error from the stream.
   *
   * <p>May only be called once and if called it must be the last method called. In particular if an
   * exception is thrown by an implementation of {@code onError} no further calls to any method are
   * allowed.
   *
   * <p>{@code t} should be a {@link io.grpc.StatusException} or {@link
   * io.grpc.StatusRuntimeException}, but other {@code Throwable} types are possible. Callers should
   * generally convert from a {@link io.grpc.Status} via {@link io.grpc.Status#asException()} or
   * {@link io.grpc.Status#asRuntimeException()}. Implementations should generally convert to a
   * {@code Status} via {@link io.grpc.Status#fromThrowable(Throwable)}.
   *
   * @param t the error occurred on the stream
   */
  void onError(Throwable t);

  /**
   * Receives a notification of successful stream completion.
   *
   * <p>May only be called once and if called it must be the last method called. In particular if an
   * exception is thrown by an implementation of {@code onCompleted} no further calls to any method
   * are allowed.
   */
  void onCompleted();
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-18 02:39:13

您可以添加ServerInterceptorServerStreamTracer.Factory来观察流事件。

或更改日志级别以获取所有事件的更多详细信息,如下所示:

代码语言:javascript
复制
    public static void main(String[] args) throws InterruptedException{
        setLogger("io.grpc");
    }
    
    private static void setLogger(String className) {
        Logger logger = Logger.getLogger(className);
        logger.setLevel(Level.ALL);

        ConsoleHandler handler = new ConsoleHandler();
        handler.setLevel(Level.ALL);
        logger.addHandler(handler);
    }

如果您希望使用logback打印日志,只需参考此文档:grpc-java-example#log

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69998348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档