I have my application running in GAE. This application makes REST call to my CloudML.
Here is the code for that
GoogleCredential credential = GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(CLOUDML_SCOPE));
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
credential);
GenericUrl url = new GenericUrl(cloudMLRestUrl);
JacksonFactory jacksonFactory = new JacksonFactory();
JsonHttpContent jsonHttpContent = new JsonHttpContent(jacksonFactory, getPayLoad());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
jsonHttpContent.setWrapperKey("instances");
jsonHttpContent.writeTo(baos);
LOG.info("Executing request... " + baos.toString());
HttpRequest request = requestFactory.buildPostRequest(url, jsonHttpContent);
HttpResponse response = request.execute();
Often the above code results in ReadTimeout exception.
java.net.SocketTimeoutException: Read timed out at
java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_121] at
java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
~[na:1.8.0_121] at
java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_121]
at
It seems we can add HttpRequestInitializer with custom timeout, but we need to pass GoogleCredential while create HttpRequestFactory
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(GoogleCredential);
Hence I can't use custom HTTPRequestInitializer. How can I increase the readTimeout for HttpRequestFactory created using GoogleCredential HTTPRequestInitializer?
HttpRequestInitializer requestInitializer = request -> { credential.initialize(request); request.setReadTimeout(0);}; HttpRequestFactory requestFactory = httpTransport.createRequestFactory(requestInitializer);
– Pape