2016-08-08 7 views
1

Wenn ich mit s3 auf Amazon arbeite, bekomme ich immer die hässliche ConnectionPoolTimeoutException.Amazon S3-Dienst ConnectionPoolTimeoutException: Timeout wartet auf Verbindung vom Pool

Das Problem ist, dass aufgrund des vorderen Endes der Anwendung, kann ich nicht die geöffneten s3 Objekte schließen, bevor vorderen Ende mit ihnen geschehen ist, so habe ich diese Lösung implementiert:

@Autowired 
private AmazonS3 s3client; // credentials are set properly. 

private static List<S3Object> openedObjects = new ArrayList<S3Object>(); 

// initialize bucket : 
private String bucketName = "myShinyNewBucket"; 
private synchronized boolean initBucket(){ 
    try{ 
     Boolean exists = null; 
     try{ 
      exists = s3client.doesBucketExist(bucketName); 
     }catch(Exception e1){ 
      System.out.println("\n\n\tToo many opened objects ; closing...\n\n"); 
      deleteOpenedS3Objects(); 
      exists = s3client.doesBucketExist(bucketName); 
     } 
     if(exists!=null){ 
      if(!exists){ 
       s3client.createBucket(new CreateBucketRequest(bucketName)); 
      } 
      return true; 
     } 
    } 
    catch(Exception e){ 
     System.out.println("\n\n\tFailed to initialize bucket.\n"); 
     e.printStackTrace(); 
    } 
    return false; 
} 

private synchronized void deleteOpenedS3Objects(){ 
    System.out.println("\n\tClosing opened objects..."); 
    try{ 
     for(int i=0 ; i<openedObjects.size() ; i++){ 
      openedObjects.get(i).close(); 
      openedObjects.remove(i); 
     } 
    }catch(Exception e1){ 
     System.out.println("\tCould not close all opened s3 objects, only the first "+i); 
    } 
    System.out.println("\tTrying again :\n\n"); 
} 

// GET : 
public final String getFromAWS(final String amazonName){ 
    S3Object s3object = null; 

    if(initBucket()){ 
     try{ 
      try{ 
       s3object = s3client.getObject(new GetObjectRequest(bucketName, amazonName)); 
      }catch(AmazonClientException e){ 
       deleteOpenedS3Objects(); 
       s3object = s3client.getObject(new GetObjectRequest(bucketName, amazonName)); 
      } 
      openedObjects.add(s3object); 
      return s3object.getObjectContent().getHttpRequest().getURI().toString(); 
     }catch(Exception e1){ 
      if (((AmazonS3Exception)e1).getStatusCode() == HttpStatus.SC_NOT_FOUND){ 
       System.out.println("\n\nNo such object in bucket.\n"); 
      } 
      else{ 
       System.out.println("\n\n\tCould not read bject from bucket.\n\n"); 
       e1.printStackTrace(); 
      } 
     } 
    } 
    return null; 
} 

Doch die Ausnahme passiert immer noch.

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool 
     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:286) ~[httpclient-4.5.1.jar!/:4.5.1] 
     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263) ~[httpclient-4.5.1.jar!/:4.5.1] 
     at sun.reflect.GeneratedMethodAccessor144.invoke(Unknown Source) ~[na:na] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91] 
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91] 
     at com.amazonaws.http.conn.ClientConnectionRequestFactory$Handler.invoke(ClientConnectionRequestFactory.java:70) ~[aws-java-sdk-core-1.11.8.jar!/:na] 
     at com.amazonaws.http.conn.$Proxy188.get(Unknown Source) ~[na:na] 
... 

Nur wenn ich ctrl + c in der Konsole tun, wird es zu dem Teil, wo es die geöffneten s3 Verbindungen endet:

... Caused by: java.lang.InterruptedException: Operation interrupted 
     at org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:142) ~[httpcore-4.4.4.jar!/:4.4.4] 
     at org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:306) ~[httpcore-4.4.4.jar!/:4.4.4] 
     at org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64) ~[httpcore-4.4.4.jar!/:4.4.4] 
     at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:192) ~[httpcore-4.4.4.jar!/:4.4.4] 
     at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:185) ~[httpcore-4.4.4.jar!/:4.4.4] 
     at org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:107) ~[httpcore-4.4.4.jar!/:4.4.4] 
     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:276) ~[httpclient-4.5.1.jar!/:4.5.1] 
     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263) ~[httpclient-4.5.1.jar!/:4.5.1] 
     at sun.reflect.GeneratedMethodAccessor144.invoke(Unknown Source) ~[na:na] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91] 
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91] 
     at com.amazonaws.http.conn.ClientConnectionRequestFactory$Handler.invoke(ClientConnectionRequestFactory.java:70) ~[aws-java-sdk-core-1.11.8.jar!/:na] 
     at com.amazonaws.http.conn.$Proxy188.get(Unknown Source) ~[na:na] 
     at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190) ~[httpclient-4.5.1.jar!/:4.5.1] 
     ... 148 common frames omitted 


     Too many opened objects. // <-- This is where it catches it. 



     Closing opened objects... 
     Could not close all opened s3 objects. 
     Trying again : 




     Failed to initialize bucket. 

Noch einmal, ich bin leider nicht in einer Position, wo ich kann geöffnete s3objects schließen, bevor ich die Funktionen in meiner s3-client-Klasse belasse. Die einzige Hoffnung, die ich hatte, war zu warten, bis TimeoutException passiert, fange es und schließe dann alle geöffneten Objekte und versuche es erneut. Allerdings kann ich nicht scheinen, es an der richtigen Stelle zu fangen.

Bitte, helfen Sie.

Vielen Dank.

Antwort

0

Ich denke, die Lösung ist, die hässliche TimeoutException in meiner @ControllerAdvice-Klasse zu fangen und zu verarbeiten. Ich habe es getan und bis jetzt habe ich es nicht in der App passiert. Ich werde eine Bestätigung posten, sobald ich sicher bin.

Verwandte Themen