Skip to content
Merged
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 @@ -15,6 +15,8 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@Service
Expand All @@ -28,43 +30,66 @@ public class MainService {

public MainResponseDto getMainInfo(UserEntity userInfo, String yearMonth) {

// 공지사항 불러오기 및 매핑 (각 카테고리별 상위 6개씩)
MainResponseDto.Notices notices = new MainResponseDto.Notices(
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "").getNotices()),
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "0").getNotices()),
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "1").getNotices()),
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "2").getNotices()),
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "4").getNotices())
);
try {
// 공지사항 비동기 호출
CompletableFuture<List<MainResponseDto.NoticeItem>> generalNotices = CompletableFuture.supplyAsync(() ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. CompletableFuture.supplyAsync()를 사용하여 공지사항 데이터를 병렬로 가져옴
  2. 각 카테고리("", "0", "1", "2", "4")의 데이터를 별도로 호출하여 병렬 처리

mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "").getNotices()));
CompletableFuture<List<MainResponseDto.NoticeItem>> category0Notices = CompletableFuture.supplyAsync(() ->
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "0").getNotices()));
CompletableFuture<List<MainResponseDto.NoticeItem>> category1Notices = CompletableFuture.supplyAsync(() ->
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "1").getNotices()));
CompletableFuture<List<MainResponseDto.NoticeItem>> category2Notices = CompletableFuture.supplyAsync(() ->
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "2").getNotices()));
CompletableFuture<List<MainResponseDto.NoticeItem>> category4Notices = CompletableFuture.supplyAsync(() ->
mapToNoticeItems(noticeService.getKwHomeNotices(1, "", "4").getNotices()));

// 설문조사 불러오기 (최대 4개)
List<SurveyResponseDto.SurveyItemDto> surveys = (surveyService.getSurveys("", 0) != null)
? surveyService.getSurveys("", 0).getSurveys().stream()
.limit(4)
.collect(Collectors.toList())
: List.of();
// 모든 공지사항 작업 완료 대기
CompletableFuture.allOf(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • CompletableFuture.allOf()로 동기화 -> 모든 CompletableFuture 작업이 완료될 때까지 대기

generalNotices, category0Notices, category1Notices,
category2Notices, category4Notices
).join();

// 캘린더 이벤트 불러오기 - 캘린더 이벤트는 로그인한 사용자만 조회 가능
CalendarResponseDto.EventGroup events = null;
if (userInfo != null) {
CalendarResponseDto calendarResponseDto = calendarService.getCalendarEvents(userInfo, yearMonth);
events = (calendarResponseDto != null) ? calendarResponseDto.getEvents() : new CalendarResponseDto.EventGroup();
}
// 공지사항 결과 병합
MainResponseDto.Notices notices = new MainResponseDto.Notices(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Future.get()를 사용하여 각 결과를 가져와 MainResponseDto.Notices 객체로 병합

generalNotices.get(),
category0Notices.get(),
category1Notices.get(),
category2Notices.get(),
category4Notices.get()
);

// 설문조사 불러오기 (최대 4개)
List<SurveyResponseDto.SurveyItemDto> surveys = (surveyService.getSurveys("", 0) != null)
? surveyService.getSurveys("", 0).getSurveys().stream()
.limit(4)
.collect(Collectors.toList())
: List.of();

// 캘린더 이벤트 불러오기 - 캘린더 이벤트는 로그인한 사용자만 조회 가능
CalendarResponseDto.EventGroup events = null;
if (userInfo != null) {
CalendarResponseDto calendarResponseDto = calendarService.getCalendarEvents(userInfo, yearMonth);
events = (calendarResponseDto != null) ? calendarResponseDto.getEvents() : new CalendarResponseDto.EventGroup();
}

// 팀플 모집 (최대 4개)
Map<String, Object> result = teamService.getOnGoingProjects(0, "");
List<ProjectListResponseDTO> projects = result.get("projects") != null
? ((List<ProjectListResponseDTO>) result.get("projects")).stream()
.limit(4)
.toList()
: List.of();
// 팀플 모집 (최대 4개)
Map<String, Object> result = teamService.getOnGoingProjects(0, "");
List<ProjectListResponseDTO> projects = result.get("projects") != null
? ((List<ProjectListResponseDTO>) result.get("projects")).stream()
.limit(4)
.toList()
: List.of();

return MainResponseDto.builder()
.notices(notices)
.surveys(surveys)
.events(events)
.projects(projects)
.build();
return MainResponseDto.builder()
.notices(notices)
.surveys(surveys)
.events(events)
.projects(projects)
.build();

} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("공지사항 병렬 처리 중 오류 발생", e);
}
}

// == Private Methods ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ public class SurveyController {
public ResponseEntity<?> getSurveyList(@RequestParam(value = "search", required = false) String search,
@RequestParam(value = "page", defaultValue = "0") int page) {
try {
// 시작 시간 측정
long startTime = System.nanoTime();
SurveyResponseDto response = surveyService.getSurveys(search, page);
// 끝 시간 측정
long endTime = System.nanoTime();

// 실행 시간 계산 (밀리초 단위로 변환)
double executionTime = (endTime - startTime) / 1_000_000_000.0;;
System.out.println(executionTime + " s");
return ResponseEntity.ok().body(Map.of("code", 200, "data", response));
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(Map.of("code", 400, "message", e.getMessage()));
Expand Down