2016-08-08 40 views
1

Amazonでs3を操作すると、醜いConnectionPoolTimeoutExceptionが発生し続けます。Amazon S3サービスConnectionPoolTimeoutException:プールからの接続を待っているタイムアウト

@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; 
} 

しかし、例外を:

問題は、私は、このソリューションを実装しているフロントエンドは、彼らと一緒に行われる前に起因するアプリケーションのフロントエンドには、私が開いてS3オブジェクトを閉じることができないということですまだ起こっている。

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] 
... 

私はコンソールでCtrl + Cを行う場合にのみ、それが開かれたS3コネクションクローズされる部分を取得ん:

再び
... 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. 

を、私は私の位置で、残念ながらないです私はs3クライアントクラスに関数を残す前に、開いたs3オブジェクトを閉じることができます。私が持っていた唯一の希望は、TimeoutExceptionが発生するまで待ってからキャッチし、開いているすべてのオブジェクトを閉じてもう一度やり直すことでした。 しかし、私は正しい場所でそれをキャッチするように見えることはできません。

お願いします。

ありがとうございます。

答えて

0

解決策は、@ControllerAdviceクラスの醜いTimeoutExceptionを捕まえて処理することだと思います。私はそうして、今のところ私はそれがアプリで起こっていない。私は確信したら確認を投稿します。

関連する問題