java - Channel ManagedChannelImpl 未正确关闭
问题描述
如果我按照这两个测试运行,我会得到错误。
第一次测试
@Rule
public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
@Test
public void findAll() throws Exception {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
// Create a server, add service, start, and register for automatic graceful shutdown.
grpcCleanup.register(InProcessServerBuilder
.forName(serverName)
.directExecutor()
.addService(new Data(mockMongoDatabase))
.build()
.start());
// Create a client channel and register for automatic graceful shutdown.
RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(
grpcCleanup.register(InProcessChannelBuilder
.forName(serverName)
.directExecutor()
.build()));
RoleOuter.Response response = stub.findAll(Empty.getDefaultInstance());
assertNotNull(response);
}
第二次测试
@Test
public void testFindAll() {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
.usePlaintext()
.build();
RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(channel);
RoleOuter.Response response = stub.findAll(Empty.newBuilder().build());
assertNotNull(response);
}
io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue SEVERE: ~ ~ ~ Channel ManagedChannelImpl{logId=1, target=localhost:8081} 没有正确关闭!!!~ ~ ~ 确保调用 shutdown()/shutdownNow() 并等待 awaitTermination() 返回 true。
java.lang.RuntimeException:位于 io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference 的 ManagedChannel 分配站点。(ManagedChannelOrphanWrapper.java:94)
如果我注释掉其中一个,那么没有错误,单元测试虽然通过,但如果两者一起运行,则会引发异常。
编辑
根据建议。
@Test
public void testFindAll() {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
.usePlaintext()
.build();
RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(channel);
RoleOuter.Response response = stub.findAll(Empty.newBuilder().build());
assertNotNull(response);
channel.shutdown();
}
解决方案
嘿,我刚刚使用 Dialogflow V2 Java SDK 遇到了类似的问题,我收到了错误
Oct 19, 2019 4:12:23 PM io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue
SEVERE: *~*~*~ Channel ManagedChannelImpl{logId=41, target=dialogflow.googleapis.com:443} was not shutdown properly!!! ~*~*~*
Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
此外,拥有庞大的客户群,我们开始遇到out of memory unable to create native thread
错误。
在执行了大量的调试操作和使用 Visual VM 线程监控后,我终于发现问题是因为SessionsClient
没有关闭。所以我使用附加的代码块来解决这个问题。测试该块后,我终于能够释放所有使用的线程,并且前面提到的错误也得到了解决。
SessionsClient sessionsClient = null;
QueryResult queryResult = null;
try {
SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
SessionsSettings sessionsSettings = settingsBuilder
.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
sessionsClient = SessionsClient.create(sessionsSettings);
SessionName session = SessionName.of(projectId, senderId);
com.google.cloud.dialogflow.v2.TextInput.Builder textInput = TextInput.newBuilder().setText(message)
.setLanguageCode(languageCode);
QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
queryResult = response.getQueryResult();
} catch (Exception e) {
e.printStackTrace();
}
finally {
sessionsClient.close();
}
推荐阅读
- reactjs - Ionic React Typescript - 将包含字符串和列表的对象传递给路由器
- c++ - 在 switch 语句中使用 rand()
- java - 在具有接口的条件下将 bean 添加到应用程序 spring 上下文
- reactjs - 如何在 Redux/Redux-thunk 中获取 api?
- python - 使用 pip 安装时忽略一些依赖项
- google-cloud-platform - 谷歌云监控和每个事件的通知数量
- ios - Mapbox 表达式检查 MGLStyleLayer 的属性并与字符串进行比较
- ms-project - 如何使用其他日期字段导入实际完成...是否有“规则”?
- string - 如何比较数字分组并返回最大的数字?
- yaml - 从 Azure Pipeline .yaml 清除退出?