Change READ TIMEOUT for a batch HTTP Request on using Google API Client Library for Java
Asked Answered
S

1

8

I am making batch requests for adding members to groups. For this I am using OAuth2.0 and obtaining the object of class type Credential.When executed, the batch.execute() throws a

java.net.SocketTimeoutException : Read timed out

To change the timeout limit, this is the solution I found : GoogleApps client giving SocketTimeOutException only

But I am not able to change the timeout of the Credential object I am creating.

Credential credential = new AuthorizationCodeInstalledApp(
                flow, new LocalServerReceiver()).authorize("user");

NOTE : Credential implements HttpRequestInitializer.

Thanks in advance!

Spermiogenesis answered 9/7, 2015 at 8:56 Comment(0)
S
9

Found this after a long time! There is a separate HttpRequestInitializer object for a batch HTTP and an atomic HTTP request to Google. I had earlier changed the HttpRequestInitializer object of the atomic requests (expecting the same to have affect on batch requests) as below:

new Directory.Builder(
                HTTP_TRANSPORT, JSON_FACTORY,new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest httpRequest) throws IOException {
                credential.initialize(httpRequest);
                httpRequest.setConnectTimeout(3);  // 3 minutes connect timeout
                httpRequest.setReadTimeout(3);  // 3 minutes read timeout
                System.out.println("Hello"); // Just to track when a http request is made.

            }
        }).setApplicationName(APPLICATION_NAME).build();

This resulted in the set read timeout for atomic requests. The batch request, sent using batch.execute() still had the default read timeout. To change the same for the batch requests, I changed this in my BatchRequest intializaiton :

BatchRequest batch = service.batch(new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest request) throws IOException {
                    credential.initialize(request);
                    request.setConnectTimeout(10 * 60000);
                    request.setReadTimeout(10 * 60000);
                    System.out.println(request.getReadTimeout() + 2); //Just to track when a batch http request is made.
                }
            });
Spermiogenesis answered 9/7, 2015 at 12:11 Comment(1)
The timeout values should be in milliseconds.Corrianne

© 2022 - 2024 — McMap. All rights reserved.