Setting ReadTimeout in GoogleCredential

1 view
Skip to first unread message

sag via StackOverflow

unread,
May 3, 2017, 5:42:11 AM5/3/17
to google-appengin...@googlegroups.com

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?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/43756456/setting-readtimeout-in-googlecredential
Reply all
Reply to author
Forward
0 new messages