-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 비동기 작업 중 발생하는 예외를 처리하는 핸들러 작성 * chore: 비동기 처리를 위한 설정 클래스 작성 * feat: 파일 관련 로직 비동기 처리 * chore: 스레드 풀 크기를 기본 설정으로 변경 및 스레드 이름 prefix 변경 * style: 사용하지 않는 import 문 제거 * refactor: 비동기 작업을 수행하는 TalkPickFileHandler를 추가하여 파일 관련 로직을 TalkPickService에서 분리 * chore: ThreadPoolTaskExecutor의 corePoolSize를 2로 설정 * chore: corePoolSize를 1로 변경 * feat: 비동기 작업에서 발생한 예외의 스택 트레이스 출력하는 코드 추가 * refactor: TalkPickService가 FileRepository를 직접 의존하지 않도록 코드 구조 개선 * refactor: 스택 트레이스를 log.error에 포함 * chore: threadPoolTaskExecutor의 corePoolSize를 default 값으로 설정 * test: TalkPickService에 TalkPickFileHandler mocking
- Loading branch information
Showing
7 changed files
with
150 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package balancetalk.global.config; | ||
|
||
import balancetalk.global.exception.CustomAsyncUncaughtExceptionHandler; | ||
import java.util.concurrent.Executor; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setThreadNamePrefix("Async task - "); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return new CustomAsyncUncaughtExceptionHandler(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/balancetalk/global/exception/CustomAsyncUncaughtExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package balancetalk.global.exception; | ||
|
||
import java.lang.reflect.Method; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
|
||
@Slf4j | ||
public class CustomAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler { | ||
|
||
@Override | ||
public void handleUncaughtException(Throwable ex, Method method, Object... params) { | ||
log.error("exception message - {} {}", ex.getMessage(), ex.getStackTrace()); | ||
log.error("method name - {}", method.getName()); | ||
for (Object param : params) { | ||
log.error("param value - {}", param); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/balancetalk/talkpick/domain/TalkPickFileHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package balancetalk.talkpick.domain; | ||
|
||
import static balancetalk.file.domain.FileType.TALK_PICK; | ||
|
||
import balancetalk.file.domain.File; | ||
import balancetalk.file.domain.FileHandler; | ||
import balancetalk.file.domain.repository.FileRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.retry.annotation.Backoff; | ||
import org.springframework.retry.annotation.Retryable; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class TalkPickFileHandler { | ||
|
||
private final FileRepository fileRepository; | ||
private final FileHandler fileHandler; | ||
|
||
@Async | ||
@Retryable(backoff = @Backoff(delay = 1000)) | ||
@Transactional | ||
public void handleFilesOnTalkPickCreate(List<Long> fileIds, Long talkPickId) { | ||
relocateFiles(fileIds, talkPickId); | ||
} | ||
|
||
private void relocateFiles(List<Long> fileIds, Long talkPickId) { | ||
List<File> files = fileRepository.findAllById(fileIds); | ||
fileHandler.relocateFiles(files, talkPickId, TALK_PICK); | ||
} | ||
|
||
public List<String> findImgUrlsBy(Long talkPickId) { | ||
return fileRepository.findImgUrlsByResourceIdAndFileType(talkPickId, TALK_PICK); | ||
} | ||
|
||
public List<Long> findFileIdsBy(Long talkPickId) { | ||
return fileRepository.findIdsByResourceIdAndFileType(talkPickId, TALK_PICK); | ||
} | ||
|
||
@Async | ||
@Retryable(backoff = @Backoff(delay = 1000)) | ||
@Transactional | ||
public void handleFilesOnTalkPickUpdate(List<Long> newFileIds, List<Long> deleteFileIds, Long talkPickId) { | ||
deleteFiles(deleteFileIds); | ||
newFileIds.removeIf((deleteFileIds::contains)); | ||
relocateFiles(newFileIds, talkPickId); | ||
} | ||
|
||
private void deleteFiles(List<Long> deleteFileIds) { | ||
if (deleteFileIds.isEmpty()) { | ||
return; | ||
} | ||
List<File> files = fileRepository.findAllById(deleteFileIds); | ||
fileHandler.deleteFiles(files); | ||
} | ||
|
||
@Async | ||
@Retryable(backoff = @Backoff(delay = 1000)) | ||
@Transactional | ||
public void handleFilesOnTalkPickDelete(Long talkPickId) { | ||
if (notExistsFilesBy(talkPickId)) { | ||
return; | ||
} | ||
List<File> files = fileRepository.findAllByResourceIdAndFileType(talkPickId, TALK_PICK); | ||
fileHandler.deleteFiles(files); | ||
} | ||
|
||
private boolean notExistsFilesBy(Long talkPickId) { | ||
return !fileRepository.existsByResourceIdAndFileType(talkPickId, TALK_PICK); | ||
} | ||
} |
Submodule config
updated
from 5d08cd to 95fadc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters