这是场景。有一个使用者,它使用kafka队列中的文件名并将这些文件上载到S3。
在使用者和上载者之间,有一个使用固定大小为10的阻塞队列的线程池执行程序。使用者使用文件名,并将实现Runnable的上载任务提交给线程池执行程序。
>UploaderTask.java
...
@Override
public void run() {
try {
System.out.println("Uploading...");
Uploader uploader = new Uploader();
uploader.uploadFile("/home/user/a.txt");
System.out.println("Uploaded");
} catch(...) {
...
} finally {
System.out.println("Clean up task");
}
}
...
Uploader.java
public class Uploader {
void upload(String fileName) {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.US_WEST_2)
.withCredentials(new ProfileCredentialsProvider())
.build();
PutObjectRequest putObjectRequest = new
PutObjectRequest(BUCKET_NAME, "key.txt", new File(fileName));
PutObjectResult putObjectResult = s3Client.putObject(putObjectRequest);
System.out.println("Upload done. Returning");
}
}
输出:
Uploading...
Clean up task
s3.putobject(...)之后的行似乎未在执行,并且在uploader.uploadFile(...)之后的代码也未在执行
Main.java
public class Main {
try {
System.out.println("Uploading...");
Uploader uploader = new Uploader();
uploader.uploadFile("/home/user/a.txt");
System.out.println("Uploaded");
} catch(...) {
...
} finally {
System.out.println("Clean up task");
}
}
输出:
Uploading...
Upload done. Returning
Uploaded
Clean up task
非常感谢您的帮助。