Instantiate an object throughout the Java application life cycle

3

I have an application here in Java that uses the Amazon SDK to send images to an S3 bucket.

To do this I use threads, basically it is modeled as follows:

The Mai n class has a list of threads and each list item is sent to an Executing class and it calls a Callable class that downloads an image and returns the image information to the uploader of the image.

The problem that every time I send the image I have to connect to the bucket. See:

BasicAWSCredentials credentials = new BasicAWSCredentials("Usuario", "Senha");
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
    .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .withRegion(Regions.US_EAST_1).build();

Does anyone know how to instantiate these connection objects in Main when I get to Executor when sending I'm already logged in?

    
asked by anonymous 29.09.2017 / 14:51

2 answers

2

First, encapsulate the creation of AmazonS3 :

private static AmazonS3 criarClient(String usuario, String senha) {
    BasicAWSCredentials credentials = new BasicAWSCredentials(usuario, senha);

    return AmazonS3ClientBuilder
        .standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .withRegion(Regions.US_EAST_1)
        .build();
}

And then, if the username and password are fixed, you can do this:

private static final AmazonS3 CLIENT = criarClient("Usuário", "Senha");

The AmazonS3 object is thread safe and immutable. So if the user and password are fixed, you can safely put it in a static variable only once when your class in question is being loaded, even if there are multiple threads involved

If the user and password are not fixed, just use the criarClient method once for each login / password you will use, even though the instances will be used concurrently by multiple threads.

    
29.09.2017 / 16:34
1

Just to clarify some points that were a bit confusing:

There is no "being logged in", what aws sdk is doing is getting your credentials to sign the request, you are working with http requests (without keep alive).

A simple example:

public class AmazonS3Worker implements Runnable {

    private AmazonS3 s3;

    public AmazonS3Worker(AmazonS3 s3) {
        this.s3 = s3;
    }

    @Override
    public void run() {

        //download com s3.getObject

    }

}

and on the Main:

public static void main(String [] args) {

    BasicAWSCredentials credentials = new BasicAWSCredentials("Usuario", "Senha");
    AmazonS3 s3client = AmazonS3ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withRegion(Regions.US_EAST_1).build();

    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (int i = 0; i < 10; i++) {
        Runnable worker = new AmazonS3Worker(s3client);
        executor.execute(worker);
    }

    executor.shutdown();
    while(!executor.isTerminated()) {}
    System.out.println("Done!");

}
    
29.09.2017 / 16:38