Skip to content

Instantly share code, notes, and snippets.

@magJ
Last active August 29, 2015 14:24
Show Gist options
  • Save magJ/f4cfc650658ea29b1b26 to your computer and use it in GitHub Desktop.
Save magJ/f4cfc650658ea29b1b26 to your computer and use it in GitHub Desktop.
A demo of a request made by httpclient blocking forever, despite timeouts being set.
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
public class InfiniteBlockingRequest {
public static void main(String... args) throws IOException, URISyntaxException {
new Thread(() -> {
try {
ServerSocket serverSocket = new ServerSocket(41567);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while(true){
Thread.sleep(10000);
out.write("\r\n");
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
CloseableHttpClient httpClient =
HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setSocketTimeout(15000)
.setConnectionRequestTimeout(500)
.setConnectTimeout(500).build())
.build();
httpClient.execute(new HttpPost(new URI("http://localhost:41567")));
System.out.println("Never gets here.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment