Updated HttpUrlConnectionHelper to support setting a longer read timeout. This is especially useful when expecting responses that are very big and need a long time to compute on the client.

This commit is contained in:
Simon Pamies committed 2017-12-01 18:51:36 +01:00
1 parent faf47f9d17
commit 0be5519db9
2 files changed
+12 -4

No files matched your search

@@ -4815,7 +4815,8 @@ public class SailingServiceImpl extends ProxiedRemoteServiceServlet implements S
URL base = createBaseUrl(urlAsString);
String path = "/sailingserver/spi/v1/masterdata/leaderboardgroups";
serverAddress = createUrl(base, path, query);
connection = HttpUrlConnectionHelper.redirectConnection(serverAddress);
// the response can take a very long time for MDI that include foiling data or such
connection = HttpUrlConnectionHelper.redirectConnection(serverAddress, Duration.ONE_HOUR.times(2));
getService().createOrUpdateDataImportProgressWithReplication(importOperationId, 0.02,
DataImportSubProgress.CONNECTION_ESTABLISH, 0.5);
if (compress) {
@@ -10,7 +10,11 @@ import com.sap.sse.common.Duration;
public class HttpUrlConnectionHelper {
private static final int HTTP_MAX_REDIRECTS = 5;
public static HttpURLConnection redirectConnection(URL url) throws MalformedURLException, IOException {
/**
* Redirects the connection using the <code>Location</code> header. Make sure to set
* the timeout if you expect the response to take longer.
*/
public static HttpURLConnection redirectConnection(URL url, Duration timeout) throws MalformedURLException, IOException {
HttpURLConnection connection = null;
URL nextUrl = url;
for (int counterOfRedirects = 0; counterOfRedirects <= HTTP_MAX_REDIRECTS; counterOfRedirects++) {
@@ -21,8 +25,7 @@ public class HttpUrlConnectionHelper {
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("User-Agent", "Mozilla/5.0...");
connection.setDoOutput(true);
// Initial timeout needs to be big enough to allow the first parts of the response to reach this server
connection.setReadTimeout((int) Duration.ONE_MINUTE.times(10).asMillis());
connection.setReadTimeout((int) timeout.asMillis());
if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM
|| connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = connection.getHeaderField("Location");
@@ -33,4 +36,8 @@ public class HttpUrlConnectionHelper {
}
return connection;
}
public static HttpURLConnection redirectConnection(URL url) throws MalformedURLException, IOException {
return redirectConnection(url, Duration.ONE_MINUTE.times(10));
}
}