1

Currently in my POC I am trying to get the list of gcp projects using java SDK, but when I am executing the API after 20 seconds getting "java.net.SocketTimeoutException: connect timed out"

Note: In my code already I have set the setConnectTimeout value as 3 mins.

Here is my code snippet:

 

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  GoogleCredential credential = GoogleCredential.fromStream(
      new ByteArrayInputStream(credentials.getBytes(Charset.forName("UTF-8"))));
  if (credential.createScopedRequired()) {
    credential = credential.createScoped(
        Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
  }


CloudResourceManager cloudResourceManager= new CloudResourceManager.Builder(new NetHttpTransport(), jsonFactory,
      setHttpTimeout(credential)).setApplicationName("Google-CloudResourceManagerSample/0.1").build();
 List<Project> projects = cloudResourceManager.projects().list().execute().getProjects();

private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
return httpRequest -> {
  requestInitializer.initialize(httpRequest);
  httpRequest.setConnectTimeout(3 * 60000)// 3 minutes connect timeout
      .setReadTimeout(3 * 60000);  // 3 minutes read timeout
};

}

ErrorTrace:

 java.net.SocketTimeoutException: connect timed out
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
 at 
 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
 at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
 at java.net.Socket.connect(Socket.java:607)
 at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:681)
 at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
 at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)

Can you please help me on it.

Jyothi Sony
  • 107
  • 2
  • 13

1 Answers1

0

As per documentation use below code snippet

    private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
      return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
          requestInitializer.initialize(httpRequest);
          httpRequest.setConnectTimeout(3 * 60000);  // 3 minutes connect timeout
          httpRequest.setReadTimeout(3 * 60000);  // 3 minutes read timeout
        }};

public static Analytics initializeAnalytics() throws Exception {
    // Initializes an authorized analytics service object.

    // Construct a GoogleCredential object with the service account email
    // and p12 file downloaded from the developer console.
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
        .setServiceAccountScopes(AnalyticsScopes.all())
        .build();

    // Construct the Analytics service object.
    return new Analytics.Builder(httpTransport, JSON_FACTORY,setHttpTimeout(credential))
        .setApplicationName(APPLICATION_NAME).build();

And also refer to stack overflow links link1, link2 for more details

  • Thanks for the replay, I have json file as a credentails but the above program is expecting the P12 files as a input. Are you seeing any thing wrong in my sample code? my dobut is why it is not taking my configured values(setconnectiontimeout values) – Jyothi Sony Dec 15 '21 at 11:07
  • seems your setReadTimeout is wrong.It should be `httpRequest.setReadTimeout(3 * 60000);`. Can you try by replacing it. – Badala Prashanth Dec 15 '21 at 14:11
  • I have tried the same earlier, but no change in the result. java supports method chaining, so it should not make any difference. – Jyothi Sony Dec 15 '21 at 14:32
  • httpRequest.setConnectTimeout(3); // 3 minutes connect timeout httpRequest.setReadTimeout(3); // 3 minutes read timeout try it – Lakshmi Dec 16 '21 at 04:32
  • https://stackoverflow.com/questions/31312692/change-read-timeout-for-a-batch-http-request-on-using-google-api-client-library refer this link as well it will be helpful – Lakshmi Dec 16 '21 at 04:34