Skip to content
Snippets Groups Projects
Commit 4a9a33c1 authored by ivan's avatar ivan
Browse files

synchronized the S3Client

parent 19d8cb23
No related branches found
No related tags found
No related merge requests found
...@@ -67,6 +67,7 @@ public class S3Client extends DB { ...@@ -67,6 +67,7 @@ public class S3Client extends DB {
public void cleanup() throws DBException { public void cleanup() throws DBException {
try { try {
//this.s3Client.shutdown(); //this should not be used //this.s3Client.shutdown(); //this should not be used
//this.s3Client = null;
} catch (Exception e){ } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
...@@ -93,30 +94,35 @@ public class S3Client extends DB { ...@@ -93,30 +94,35 @@ public class S3Client extends DB {
} }
/** /**
* Initialize any state for the storage. * Initialize any state for the storage.
* Called once per S3 instance; there is one S3 instance per client thread. * Called once per S3 instance; If the client is not null it is re-used.
*/ */
@Override @Override
public void init() throws DBException { public void init() throws DBException {
synchronized (S3Client.class){
Properties props = getProperties(); Properties props = getProperties();
accessKeyId = props.getProperty("s3.accessKeyId","accessKeyId"); accessKeyId = props.getProperty("s3.accessKeyId","accessKeyId");
secretKey = props.getProperty("s3.secretKey","secretKey"); secretKey = props.getProperty("s3.secretKey","secretKey");
endPoint = props.getProperty("s3.endPoint","s3.amazonaws.com"); endPoint = props.getProperty("s3.endPoint","s3.amazonaws.com");
region = props.getProperty("s3.region","us-east-1"); region = props.getProperty("s3.region","us-east-1");
maxErrorRetry = props.getProperty("s3.maxErrorRetry","15"); maxErrorRetry = props.getProperty("s3.maxErrorRetry","15");
System.out.println("Inizializing the S3 connection"); System.out.println("Inizializing the S3 connection");
s3Credentials = new BasicAWSCredentials(accessKeyId,secretKey); s3Credentials = new BasicAWSCredentials(accessKeyId,secretKey);
clientConfig = new ClientConfiguration(); clientConfig = new ClientConfiguration();
clientConfig.setMaxErrorRetry(Integer.parseInt(maxErrorRetry)); clientConfig.setMaxErrorRetry(Integer.parseInt(maxErrorRetry));
try { if (s3Client != null) {
s3Client = new AmazonS3Client(s3Credentials,clientConfig); System.out.println("Reusing the same client");
s3Client.setRegion(Region.getRegion(Regions.fromName(region))); return;
s3Client.setEndpoint(endPoint); }
System.out.println("Connection successfully initialized"); try {
} catch (Exception e){ s3Client = new AmazonS3Client(s3Credentials,clientConfig);
System.err.println("Could not connect to S3 storage because: "+ e.toString()); s3Client.setRegion(Region.getRegion(Regions.fromName(region)));
e.printStackTrace(); s3Client.setEndpoint(endPoint);
return; System.out.println("Connection successfully initialized");
} catch (Exception e){
System.err.println("Could not connect to S3 storage because: "+ e.toString());
e.printStackTrace();
return;
}
} }
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment