【问题标题】:How to manage app crashing when grpc blockingstub is called with deadline如何在截止日期调用 grpc blockstub 时管理应用程序崩溃
【发布时间】:2020-09-03 20:11:53
【问题描述】:

我正在尝试开发一个使用 grpc 阻塞存根连接到后端服务器的应用程序。

我到达了发出请求的地步,我的应用程序由于超过截止日期而崩溃。预期的行为是当应用程序崩溃和关闭不可接受时服务器无响应时,try catch 处理。

以下是我的存根生成和错误:

    public LoginDataSource() {
    }

    public Result<LoggedInUser> login(String username, String password) {
        LoginServiceGrpc.LoginServiceBlockingStub blockingStub = LoginServiceGrpc.newBlockingStub(commChannel);
        GrpcServerComm.LoginData loginData = GrpcServerComm.LoginData.newBuilder().setUsername(username).setPassword(password).build();
        GrpcServerComm.LoginRequest serverResponse = blockingStub.withDeadlineAfter(2000, TimeUnit.MILLISECONDS).getLogin(loginData);
        long serverResponseVal = 0;
        try
        {
            LoggedInUser activeUser = new LoggedInUser(username,username);
            serverResponseVal = serverResponse.getResponseVal();
            return new Result.Success<>(activeUser);
        }
        catch (Exception e)
        {
            return new Result.Error(new IOException("Error logging in", e));
        }

    }

调试:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.equinox.openeyes, PID: 2401
    io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 1766623100ns. [buffered_nanos=1774305900, waiting_for_connection]
        at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:240)
        at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:221)
        at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:140)
        at com.equinox.openeyes.LoginServiceGrpc$LoginServiceBlockingStub.getLogin(LoginServiceGrpc.java:155)

【问题讨论】:

    标签: java android grpc channel


    【解决方案1】:

    试试这个

    public MutableLiveDataResult<LoggedInUser> login(String username, String password) {
    
            MutableLiveDataResult<LoggedInUser> observeable = new MutableLiveDataResult<LoggedInUser>();
            LoginServiceGrpc.LoginServiceBlockingStub service = LoginServiceGrpc.newStub(commChannel);
            GrpcServerComm.LoginData loginData = GrpcServerComm.LoginData.newBuilder().setUsername(username).setPassword(password).build();
            service.getLogin(loginData, new StreamObserver<LoggedInUser> {
    
                public void onNext(LoggedInUser response) {
                    observeable.postValue(response);
                }
    
                public void onError(Throwable t) {
                    observeable.postValue(null);
                }
    
                public void onCompleted(Throwable t) {
                    // if you wanna close your channel
                    // commChannel.shutdown();
                } 
            })
            return observable;
        }
    

    并观察MutableLiveData 的变化并在那里得到你的结果。

    【讨论】:

    • 感谢您的帮助!我无法使用它,因为我还有其他需要定义返回类型的活动。
    【解决方案2】:

    我发现问题是我的阻塞存根调用需要在 try catch 中。

    一旦我将它移到那里并将异常更改为 StatusRuntimeExceotion,我就能够相应地处理异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多