使用Google API Client Library for Java更改批量HTTP请求的READ TIMEOUT

时间:2015-07-09 08:56:07

标签: java google-api-java-client google-admin-sdk google-directory-api

我正在批量请求将成员添加到组。为此我使用OAuth2.0并获取类类型Credential的对象。执行时,batch.execute()抛出

java.net.SocketTimeoutException : Read timed out

要更改超时限制,这是我找到的解决方案: GoogleApps client giving SocketTimeOutException only

但是我无法更改我正在创建的Credential对象的超时。

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

注意:Credential实现了HttpRequestInitializer。

提前致谢!

1 个答案:

答案 0 :(得分:5)

经过很长一段时间才发现这个!有一个单独的HttpRequestInitializer对象用于批量HTTP和一个原子HTTP请求到谷歌。我之前已经更改了原子请求的HttpRequestInitializer对象(期望它对批处理请求产生影响),如下所示:

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();

这导致原子请求的设置读取超时。使用batch.execute()发送的批处理请求仍具有默认读取超时。 要为批处理请求更改相同内容,我在BatchRequest初始化中更改了此内容:

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.
                }
            });