首页 > 解决方案 > 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++)) 并且没有抛出异常

标签: amazon-web-servicesspring-bootamazon-elastic-beanstalk

解决方案


推荐阅读