Search code examples
javagoogle-app-enginegoogle-bigquery

How to set timeout for BigQuery API request in Java


Sometimes when we poll for a BigQuery job, our request ends up with SocketTimeoutException. You can see the code raising the exception below.

this.bigquery.jobs().get(projectNumber, jobId).execute();

And here is the error message we get.

...
Caused by: java.net.SocketTimeoutException:
Timeout while fetching URL: https://www.googleapis.com/bigquery/v2/projects/######/jobs/######
...

My question is if there is a way to extend the timeout. And does anyone know what the default timeout is?


Solution

  • You can wrap the credential object in a HTTP initializer that disables (or extends) timeouts. That is, where you currently have this:

    Credential credential = ...
    Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, credential);
    

    you could do

    final Credential credential = ...
    HttpRequestInitializer initializer = new HttpRequestInitializer() {
      public void initialize(HttpRequest request) {
        credential.initialize(request);
        request.connectTimeout = request.readTimeout = 0;
      }
    }
    Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, initializer);
    

    See this for BigQuery object javadoc, this for BigQuery object creation example, and this for HttpRequestInitializer overloading.