Skip to content

Commit

Permalink
Merge branch 'dev' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyuung committed Jul 22, 2024
2 parents a23c68d + fc3897a commit 39b402d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package warehouse.common.config.async;

import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@EnableAsync
@Configuration
public class AsyncConfig {

@Bean(name = "imageUploadExecutor")
public Executor imageUploadExecutor() {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(36);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(100);
executor.initialize();

return executor;
}
}

Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package warehouse.domain.image.business;

import db.domain.image.ImageEntity;
import db.domain.image.enums.ImageKind;
import global.annotation.Business;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import warehouse.common.error.ImageErrorCode;
import warehouse.common.exception.image.ImageStorageException;
import warehouse.domain.goods.controller.model.GoodsRequest;
import warehouse.domain.goods.converter.GoodsConverter;
import warehouse.domain.image.controller.model.ImageListRequest;
import warehouse.domain.image.controller.model.ImageListResponse;
import warehouse.domain.image.controller.model.ImageRequest;
import warehouse.domain.image.controller.model.ImageResponse;
import warehouse.domain.image.converter.ImageConverter;
Expand All @@ -28,24 +31,50 @@ public class ImageBusiness {
private final ImageConverter imageConverter;
private final GoodsConverter goodsConverter;

public ImageResponse uploadImage(ImageRequest request) {
@Qualifier("imageUploadExecutor")
private final Executor executor;

public ImageResponse uploadImage(ImageRequest request) {
if (request.getFile().isEmpty()) {
throw new ImageStorageException(ImageErrorCode.IMAGE_STORAGE_ERROR);
}

return imageUploadBizLogic(request);
return imageConverter.toResponse(imageUploadBizLogic(request));
}

public List<ImageResponse> uploadImageList(ImageListRequest listRequest) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

return imageConverter.toRequestList(listRequest).stream()
.map(this::uploadImage).collect(Collectors.toList());
List<CompletableFuture<ImageEntity>> futures = imageConverter.toRequestList(listRequest).stream()
.map(it -> CompletableFuture.supplyAsync(() -> {
try {
RequestContextHolder.setRequestAttributes(requestAttributes);
return imageUploadBizLogic(it);
} finally {
RequestContextHolder.resetRequestAttributes();
}
}, executor)).toList();

return getImageResponsesFromFutures(futures);
}

private List<ImageResponse> getImageResponsesFromFutures(List<CompletableFuture<ImageEntity>> futures) {
List<ImageEntity> imageEntityList = new ArrayList<>();
futures.forEach(it -> {
try {
imageEntityList.add(it.join());
} catch (CompletionException e) {
deleteImage(imageEntityList);
throw new ImageStorageException(ImageErrorCode.IMAGE_STORAGE_ERROR);
}
});
return imageConverter.toResponseList(imageEntityList);
}

private void deleteImage(List<ImageEntity> imageEntityList) {
imageEntityList.forEach(imageService::deleteImageDB);
}

public List<ImageResponse> getImageUrlListBy(Long goodsId) {

public List<ImageResponse> getImageUrlListBy(Long goodsId) {
return imageService.getImageUrlList(goodsId).stream()
.map(imageConverter::toResponse).collect(Collectors.toList());
}
Expand All @@ -54,11 +83,9 @@ public byte[] getImageFile(String filepath) {
return imageService.getImageFileByteList(filepath);
}

private ImageResponse imageUploadBizLogic(ImageRequest request) {
private ImageEntity imageUploadBizLogic(ImageRequest request) {
ImageEntity entity = imageConverter.toEntity(request);
imageService.uploadImage(request.getFile(), entity);
ImageEntity newEntity = imageService.saveImageDataToDB(entity);
return imageConverter.toResponse(newEntity);
return imageService.saveImageDataToDB(entity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public void updateImageDB(ImageEntity th) {
saveImageDataToDB(th);
}

public void deleteImageDB(ImageEntity th) { imageRepository.deleteById(th.getId()); }

public List<ImageEntity> getImagesByImageIdList(List<Long> ids) {
return ids.stream().map(this::getImageByImageId).collect(Collectors.toList());
}
Expand Down

0 comments on commit 39b402d

Please sign in to comment.