花5秒关闭Java grpc ManagedChannel

时间:2018-07-09 20:04:50

标签: java grpc

我有一个需要与一台服务器断开连接并连接到另一台服务器的客户端。大约需要16秒。我仍然没有调试连接逻辑,但是我可以看到关闭通道需要5秒钟。这是预期的行为,还是我应该在代码中寻找线程饥饿。

LOG.debug("==============SHUTTING DOWN MANAGED CHANNEL");
long startTime=System.currentTimeMillis();
channel.shutdown().awaitTermination(20, SECONDS);
long endTime=System.currentTimeMillis();
LOG.debug("Time to shutdown channel ms = {}",endTime-startTime);
LOG.debug("==============RETURN FROM SHUTTING DOWN MANAGED CHANNEL");

从日志中

2018-07-09 14:41:23,143 DEBUG [com.ticomgeo.ftc.client.FTCClient] (EE-ManagedExecutorService-singleThreaded-Thread-1) ==============SHUTTING DOWN MANAGED CHANNEL 
2018-07-09 14:41:28,151 INFO  [io.grpc.internal.ManagedChannelImpl] (grpc-default-worker-ELG-1-1) [io.grpc.internal.ManagedChannelImpl-1] Terminated 
2018-07-09 14:41:28,152 DEBUG [com.ticomgeo.ftc.client.FTCClient] (EE-ManagedExecutorService-singleThreaded-Thread-1) Time to shutdown channel ms = 5009 
2018-07-09 14:41:28,152 DEBUG [com.ticomgeo.ftc.client.FTCClient] (EE-ManagedExecutorService-singleThreaded-Thread-1) ==============RETURN FROM SHUTTING DOWN MANAGED CHANNEL 

1 个答案:

答案 0 :(得分:0)

有两个关闭功能,shutdown和shutdownNow。您是否有机会拨打电话以阻止关机? shutdownNow可能会更好地为您服务。

关闭

启动有序关闭,在此过程中,先前存在的呼叫将继续,但新呼叫将被拒绝。

立即关闭

启动强制关机,以拒绝现有和新的呼叫。尽管强制执行,但关闭过程仍不是瞬时的。此方法返回后,isTerminated()可能会立即返回false。