Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 927
Piyush Jain Avatar asked Jul 09 '15 08:07

Piyush Jain


1 Answers

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.
                }
            });
like image 133
Piyush Jain Avatar answered Oct 05 '22 23:10

Piyush Jain