Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Virtual Thread Configuration #425

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.time.YearMonth;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -38,11 +41,14 @@ public class MissionRecordService {
private final MissionRecordRepository missionRecordRepository;
private final MissionRecordTtlRepository missionRecordTtlRepository;

private static final ExecutorService VIRTUAL_THREAD_EXECUTOR =
Executors.newVirtualThreadPerTaskExecutor();

public MissionRecordCreateResponse createMissionRecord(MissionRecordCreateRequest request) {
long diffHour = Duration.between(request.startedAt(), request.finishedAt()).toHours();
validateMissionRecordDurationOverTime(diffHour);

final Mission mission = findMissionById(request.missionId());
final Mission mission = findMissionByIdAsync(request.missionId());
final Member member = memberUtil.getCurrentMember();

Duration duration =
Expand Down Expand Up @@ -81,6 +87,12 @@ private void validateMissionRecordExistsToday(Long missionId) {
}
}

private Mission findMissionByIdAsync(Long missionId) {
return CompletableFuture.supplyAsync(
() -> findMissionById(missionId), VIRTUAL_THREAD_EXECUTOR)
.join();
}

private Mission findMissionById(Long missionId) {
return missionRepository
.findById(missionId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.depromeet.global.config.thread;

import java.util.concurrent.Executors;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.support.TaskExecutorAdapter;

@Configuration
public class ThreadConfig {

@Bean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
public AsyncTaskExecutor asyncTaskExecutor() {
return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
}

@Bean
public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
return protocolHandler ->
protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
}
}
29 changes: 23 additions & 6 deletions src/main/java/com/depromeet/global/util/MemberUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.depromeet.domain.member.domain.Member;
import com.depromeet.global.error.exception.CustomException;
import com.depromeet.global.error.exception.ErrorCode;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -13,16 +16,30 @@ public class MemberUtil {

private final SecurityUtil securityUtil;
private final MemberRepository memberRepository;
private static final ExecutorService VIRTUAL_THREAD_EXECUTOR =
Executors.newVirtualThreadPerTaskExecutor();

public Member getCurrentMember() {
return memberRepository
.findById(securityUtil.getCurrentMemberId())
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
return CompletableFuture.supplyAsync(
() ->
memberRepository
.findById(securityUtil.getCurrentMemberId())
.orElseThrow(
() ->
new CustomException(
ErrorCode.MEMBER_NOT_FOUND)))
.join();
}

public Member getMemberByMemberId(Long memberId) {
return memberRepository
.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
return CompletableFuture.supplyAsync(
() ->
memberRepository
.findById(memberId)
.orElseThrow(
() ->
new CustomException(
ErrorCode.MEMBER_NOT_FOUND)))
.join();
}
}
Loading