grpc - grpc java中ListenableFuture风格的存根和异步存根之间的区别
问题描述
gRPC java实现中ListenableFuture风格的存根和异步存根有什么区别?
哪一个用于异步通信?
public static MyServiceStub newStub(io.grpc.Channel channel) {
io.grpc.stub.AbstractStub.StubFactory<MyServiceStub> factory =
new io.grpc.stub.AbstractStub.StubFactory<MyServiceStub>() {
@java.lang.Override
public MyServiceStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new MyServiceStub(channel, callOptions);
}
};
return MyServiceStub.newStub(factory, channel);
}
public static MyServiceBlockingStub newBlockingStub(
io.grpc.Channel channel) {
io.grpc.stub.AbstractStub.StubFactory<MyServiceBlockingStub> factory =
new io.grpc.stub.AbstractStub.StubFactory<MyServiceBlockingStub>() {
@java.lang.Override
public MyServiceBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new MyServiceBlockingStub(channel, callOptions);
}
};
return MyServiceBlockingStub.newStub(factory, channel);
}
public static MyServiceFutureStub newFutureStub(
io.grpc.Channel channel) {
io.grpc.stub.AbstractStub.StubFactory<MyServiceFutureStub> factory =
new io.grpc.stub.AbstractStub.StubFactory<MyServiceFutureStub>() {
@java.lang.Override
public MyServiceFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new MyServiceFutureStub(channel, callOptions);
}
};
return MyServiceFutureStub.newStub(factory, channel);
}
解决方案
同步与异步存根对通信没有影响。grpc-java 中的存根是 Channel API 之上的一个小垫片,存根的不同之处在于它们是不同的 API。只需选择与您希望接收响应的方式最匹配的存根类型。如果您喜欢异步 API,请使用异步 API;如果您喜欢 Future API,请使用 Future API;如果您喜欢同步,请使用同步。
推荐阅读
- android - 与 ConstraintLayout 一起使用时,按钮文本不换行
- node.js - 在反应中在 POST 中发送文件(请求 api)
- javascript - 如何在 Javascript 代码中访问位于外部 PHP 文件中的 PHP 变量?
- javascript - 如何修复离子中 startTrackerWithId 上的“对象(...)不是函数”?
- python - 为什么 mypy 推断公共基类型而不是所有包含类型的并集?
- opengl - 在使用 GL_MAP_PERSISTENT_BIT 映射后,解绑从 glBufferStorage 映射的缓冲区,然后写入指针是否安全?
- node.js - 无法让 Query.find() 返回数据,即使我知道它存在
- kotlin - 响应管道无法使用 gson 转换数据类 - Ktor
- c++ - 将特征数组行保存在变量中
- react-native - 参考错误:找不到变量:weatherCondition