我正在使用 JDK 11 附带的新 HttpClient 发出许多请求(向 Github 的 API,但我认为这无关紧要),尤其是 GET。
对于每个请求,我构建并使用一个 HttpClient,如下所示:
final ExecutorService executor = Executors.newSingleThreadExecutor();
final HttpClient client = client = HttpClient
    .newBuilder()
    .followRedirects(HttpClient.Redirect.NORMAL)
    .connectTimeout(Duration.ofSeconds(10))
    .executor(executor)
    .build();
try {
   //send request and return parsed response;
} finally {
   //manually close the specified executor because HttpClient doesn't implement Closeable,
   //so I'm not sure when it will release resources.
   executor.shutdownNow();
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,除了时不时地,我收到以下异常,并且请求将不再有效,直到我重新启动应用程序:
Caused by: java.net.ConnectException: Cannot assign requested address
...
Caused by: java.net.BindException: Cannot assign requested address
    at java.base/sun.nio.ch.Net.connect0(Native Method) …Run Code Online (Sandbox Code Playgroud) 是否有可能启动Docker容器(在CMD中使用某些命令)并确保它不会停留超过指定的时间段?
例如,我有一个Ubuntu容器,我想启动它,执行一些命令并在最后退出(杀死容器).但是这些命令可能包含无限循环或者可能只持续很长时间,所以我想确保Docker容器在指定的时间后自动被杀死.