amazon-web-services - Spring Boot 任务未完成
问题描述
我正在开发一个 Spring Boot 应用程序(部署在 aws elastic beanstalk 上),其中有一些长时间运行的任务有时无法完成。我知道他们没有,因为我在开始之前和结束之后都有日志。例如,一项任务是获取包含图像的 zip 文件并将其发布到 aws S3 上。有时它会在上传所有图像之前停止。另一个任务是获取 csv 或 json 文件并更新数据库中的数据,同样的问题。
我想知道这是否是由于某些 aws 操作(例如自动缩放)或 vm 问题的性能
将图像发布到 S3 的代码示例
for(int i = 0; i < images.size(); i++) {
saveLog("uploading " + (i+1) + ".JPG");
try {
uploadedAutres += doUploadOneOfMultipleImages(product,
ImageIO.read(zipFile.getInputStream(images.get(i))), i , false); //i + product.getNombreImagesAutre()
} catch (Throwable e) {
saveLog("failed uploading " + (i+1) + ".JPG\n" + e.getMessage());
e.printStackTrace();
break;
}
}
public int doUploadOneOfMultipleImages(Product product, BufferedImage image, int i, boolean threeSixty) {
ByteArrayOutputStream largeByteArray = new ByteArrayOutputStream();
ByteArrayOutputStream mediumByteArray = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", largeByteArray);
ImageIO.write(resizeImageExample.resize(image, 434, 650), "png", mediumByteArray);
largeByteArray.close();
mediumByteArray.close();
String largeFileName = threeSixty ? "" : "autre/";
String newFileName = threeSixty ? "" : "autre/";
newFileName += product.getReference() + "/";
largeFileName += product.getReference() + "/large_";
if(i+1 <= 9) {
newFileName += "0";
largeFileName += "0";
}
newFileName += i+1;
newFileName += ".jpg";
largeFileName += i+1;
largeFileName += ".jpg";
objectOperations.upload(bucketName, newFileName, mediumByteArray.toByteArray(), "image/jpeg");
objectOperations.upload(bucketName, largeFileName, largeByteArray.toByteArray(), "image/jpeg");
return 1;
} catch (Exception e) {
return 0;
}
}
public void upload(String bucketName, String objectKey, byte[] objectBytes, String contentType) throws Exception {
String result = putS3Object(s3Client, bucketName, objectKey, objectBytes, contentType);
}
public String putS3Object(S3Client s3, String bucketName, String objectKey, byte[] objectBytes, String contentType) throws Exception {
PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.contentType(contentType)
.build(),
RequestBody.fromBytes(objectBytes));
return response.eTag();
}
当它停止时,它处于循环中间 (for(int i = 0; i < images.size(); i++)) 并且没有抛出异常
解决方案
推荐阅读
- git - Sublime 在更改分支时不会自动重新加载树和文件 - 通过 WSL 工作
- javascript - 如何更改select2 jquery插件中下拉项的属性
- java - 为什么我在文件输入/输出的 java 程序中收到此错误?
- jquery - 在 jquery 数据表上使用引导样式时的悬停效果
- mongoose - 从另一个项目中查找 mongoose 文档
- android - Android Studio:无法解析配置“:classpath”的所有工件
- javascript - 有没有办法更好地组织盒子?装箱问题
- sql - SQL Server:返回特定格式的字符串
- java - 数组不承认元素为链表
- python - Amazon MWAA 和 ec2 实例之间的 SSH 连接