Skip to content

Commit

Permalink
refactor: 비동기 처리용 Executor 분리, Firebase ThreadPool 커스텀, 알림 재전송 실패 로직 …
Browse files Browse the repository at this point in the history
…추가, 콜백 메소드를 활용한 Non-Blocking 처리 (#501)

* chore: 테스트 메서드 주석 해제

* fix: 이벤트 발행 인자 객체 수정

* feat: Listener를 이용해 블락킹 요소 제거

* feat: 알림 실패 시 재시도 로직 추가

* feat: firebase SDK 내부에서 사용하는 스레드 풀 설정

* feat: 스레드 풀 분리에 따른 빈 이름 명시

* style: 컨벤션에 맞도록 코드 수정

* chore: CustomThreadManager 패키지 분리

* chore: 테스트 API 비공개 처리

* docs: fcm docs java doc 추가

---------

Co-authored-by: DongUk <[email protected]>
  • Loading branch information
rawfishthelgh and Kim0914 authored May 27, 2024
1 parent 04f7066 commit 3ad217b
Show file tree
Hide file tree
Showing 44 changed files with 267 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.official.pium.admin.service;

import com.official.pium.admin.mapper.AdminMapper;
import com.official.pium.admin.domain.Admin;
import com.official.pium.admin.mapper.AdminMapper;
import com.official.pium.admin.service.dto.AdminLoginRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.official.pium.admin.service;

import com.official.pium.admin.domain.Admin;
import com.official.pium.admin.domain.Registration;
import com.official.pium.admin.service.dto.RegistrationRequest;
import com.official.pium.admin.service.dto.RegistrationResponse;
import com.official.pium.admin.mapper.RegistrationMapper;
import com.official.pium.admin.repository.RegistrationRepository;
import com.official.pium.admin.domain.Admin;
import com.official.pium.admin.service.dto.RegistrationRequest;
import com.official.pium.admin.service.dto.RegistrationResponse;
import com.official.pium.common.dto.DataResponse;
import com.official.pium.common.exception.AuthorizationException;
import com.official.pium.common.util.PhotoManager;
import com.official.pium.common.dto.DataResponse;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.official.pium.admin.service;

import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.petPlant.event.notification.NotificationEvent;
import com.official.pium.petPlant.repository.PetPlantRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -16,63 +19,49 @@ public class TestService {
private final PetPlantRepository petPlantRepository;
private final ApplicationEventPublisher publisher;

// public void sendWaterNotificationTest() {
// List<PetPlant> petPlants = petPlantRepository.findAllByMemberId(7L);
// List<NotificationEvent> events = petPlants.stream()
// .map(plant -> NotificationEvent.builder()
// .title(plant.getNickname())
// .body("(테스트 중) 물을 줄 시간이에요!")
// .deviceToken(plant.getMember().getDeviceToken())
// .build()
// ).toList();
// log.info("동기 알림 테스트 시작. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
// publisher.publishEvent(NotificationEvents.from(events));
// log.info("동기 알림 테스트 종료. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
// }
public void sendWaterNotificationAsyncRampTest() {
List<PetPlant> petPlants = petPlantRepository.findAllByMemberId(7L);
List<NotificationEvent> events = petPlants.stream()
.map(plant -> NotificationEvent.builder()
.title(plant.getNickname())
.body("(테스트 중) 물을 줄 시간이에요!")
.deviceToken(plant.getMember().getDeviceToken())
.build()
).toList();

// public void sendWaterNotificationAsyncRampTest() {
// List<PetPlant> petPlants = petPlantRepository.findAllByMemberId(7L);
// List<NotificationEvent> events = petPlants.stream()
// .map(plant -> NotificationEvent.builder()
// .title(plant.getNickname())
// .body("(테스트 중) 물을 줄 시간이에요!")
// .deviceToken(plant.getMember().getDeviceToken())
// .build()
// ).toList();
for (int i = 0; i < 100; i++) {
PetPlant petPlant = petPlants.get(i);
NotificationEvent event = NotificationEvent.builder()
.title(petPlant.getNickname())
.body("물줘")
.deviceToken(petPlant.getMember().getDeviceToken())
.build();
publisher.publishEvent(event);
}

// for (int i = 0; i < 100; i++) {
// PetPlant petPlant = petPlants.get(i);
// NotificationEvent event = NotificationEvent.builder()
// .title(petPlant.getNickname())
// .body("물줘")
// .deviceToken(petPlant.getMember().getDeviceToken())
// .build();
// publisher.publishEvent(event);
// }
log.info("비동기 테스트 램프업 시작");
for (int i = 0; i < 100; i++) {
NotificationEvent notificationEvent = events.get(i);
publisher.publishEvent(notificationEvent);
}
}

// log.info("비동기 테스트 램프업 시작");
// for (int i = 0; i < 100; i++) {
// NotificationEvent notificationEvent = events.get(i);
// publisher.publishEvent(notificationEvent);
// }
// }
public void sendWaterNotificationAsyncTest() {
List<PetPlant> petPlants = petPlantRepository.findAllByMemberId(7L);
List<NotificationEvent> events = petPlants.stream()
.map(plant -> NotificationEvent.builder()
.title(plant.getNickname())
.body("(테스트 중) 물을 줄 시간이에요!")
.deviceToken(plant.getMember().getDeviceToken())
.build()
).toList();

// public void sendWaterNotificationAsyncTest() {
// List<PetPlant> petPlants = petPlantRepository.findAllByMemberId(7L);
// List<NotificationEvent> events = petPlants.stream()
// .map(plant -> NotificationEvent.builder()
// .title(plant.getNickname())
// .body("(테스트 중) 물을 줄 시간이에요!")
// .deviceToken(plant.getMember().getDeviceToken())
// .build()
// ).toList();
//
// int i = 1;
// log.info("비동기 알림 테스트 시작. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
// for (NotificationEvent event : events) {
// log.info(i++ + "번째 알림 이벤트");
// publisher.publishEvent(event);
// }
// log.info("비동기 알림 테스트 종료. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
// }
int i = 1;
log.info("비동기 알림 테스트 시작. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
for (NotificationEvent event : events) {
log.info(i++ + "번째 알림 이벤트");
publisher.publishEvent(event);
}
log.info("비동기 알림 테스트 종료. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.official.pium.admin.ui;

import com.official.pium.admin.domain.Admin;
import com.official.pium.admin.service.RegistrationService;
import com.official.pium.admin.service.dto.RegistrationRequest;
import com.official.pium.admin.service.dto.RegistrationResponse;
import com.official.pium.admin.service.RegistrationService;
import com.official.pium.admin.domain.Admin;
import com.official.pium.member.ui.Auth;
import com.official.pium.common.dto.DataResponse;
import com.official.pium.member.ui.Auth;
import jakarta.validation.constraints.Positive;
import java.net.URI;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.official.pium.common.ui;

import com.official.pium.member.ui.Auth;
import com.official.pium.member.domain.Member;
import com.official.pium.common.exception.AuthenticationException;
import com.official.pium.member.domain.Member;
import com.official.pium.member.repository.MemberRepository;
import com.official.pium.member.ui.Auth;
import com.official.pium.sessionGroup.application.SessionGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.official.pium.common.util;

import java.io.File;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Component
public class PhotoLocalManager implements PhotoManager {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.official.pium.config;

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

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
public class AsyncConfig {

@Override
public Executor getAsyncExecutor() {
@Bean("notificationAsyncExecutor")
public TaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(40);
executor.setThreadNamePrefix("2024-Pium-Thread: ");
executor.initialize();
return executor;
}

@Bean("notificationCallBackExecutor")
public TaskExecutor getCallBackExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setThreadNamePrefix("2024-Pium-Call-Back-Thread: ");
executor.initialize();
return executor;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.official.pium.dictionaryPlant.application;

import com.official.pium.dictionaryPlant.domain.DictionaryPlant;
import com.official.pium.dictionaryPlant.repository.DictionaryPlantRepository;
import com.official.pium.petPlant.repository.PetPlantRepository;
import com.official.pium.common.dto.DataResponse;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantCreateRequest;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantResponse;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantSearchResponse;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantUpdateRequest;
import com.official.pium.dictionaryPlant.domain.DictionaryPlant;
import com.official.pium.dictionaryPlant.repository.DictionaryPlantRepository;
import com.official.pium.petPlant.repository.PetPlantRepository;
import java.util.List;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.official.pium.dictionaryPlant.application.dto;

import com.official.pium.dictionaryPlant.domain.DictionaryPlant;
import com.official.pium.petPlant.domain.vo.WaterCycle;
import com.official.pium.dictionaryPlant.domain.vo.CareDetail;
import com.official.pium.dictionaryPlant.domain.vo.Classification;
import com.official.pium.dictionaryPlant.domain.vo.Property;
import com.official.pium.petPlant.domain.vo.Temperature;
import com.official.pium.petPlant.domain.vo.WaterCycle;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.official.pium.dictionaryPlant.application.dto;

import com.official.pium.petPlant.domain.vo.WaterCycle;
import com.official.pium.dictionaryPlant.domain.vo.CareDetail;
import com.official.pium.dictionaryPlant.domain.vo.Classification;
import com.official.pium.dictionaryPlant.domain.vo.Property;
import com.official.pium.petPlant.domain.vo.Temperature;
import com.official.pium.petPlant.domain.vo.WaterCycle;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import com.official.pium.admin.domain.Admin;
import com.official.pium.admin.ui.AdminAuth;
import com.official.pium.dictionaryPlant.application.DictionaryPlantService;
import com.official.pium.common.dto.DataResponse;
import com.official.pium.dictionaryPlant.application.DictionaryPlantService;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantCreateRequest;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantResponse;
import com.official.pium.dictionaryPlant.application.dto.DictionaryPlantSearchResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.official.pium.garden.application;

import com.official.pium.garden.application.dto.GardenCreateRequest;
import com.official.pium.garden.application.dto.GardenResponse;
import com.official.pium.garden.domain.Garden;
import com.official.pium.member.domain.Member;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.garden.domain.vo.GardenPlantState;
import com.official.pium.garden.repository.GardenRepository;
import com.official.pium.member.domain.Member;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.petPlant.repository.PetPlantRepository;
import com.official.pium.garden.application.dto.GardenCreateRequest;
import com.official.pium.garden.application.dto.GardenResponse;
import java.time.LocalDate;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.official.pium.garden.application.dto;

import com.official.pium.garden.domain.Garden;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.garden.domain.vo.GardenPlantState;
import com.official.pium.petPlant.domain.PetPlant;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.official.pium.common.domain.BaseEntity;
import com.official.pium.dictionaryPlant.domain.DictionaryPlant;
import com.official.pium.member.domain.Member;
import com.official.pium.garden.domain.vo.GardenPlantState;
import com.official.pium.member.domain.Member;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.official.pium.garden.ui;

import com.official.pium.member.ui.Auth;
import com.official.pium.member.domain.Member;
import com.official.pium.garden.application.GardenService;
import com.official.pium.garden.application.dto.GardenCreateRequest;
import com.official.pium.garden.application.dto.GardenResponse;
import com.official.pium.member.domain.Member;
import com.official.pium.member.ui.Auth;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.official.pium.history.application;

import com.official.pium.petPlant.event.history.HistoryEvent;
import com.official.pium.petPlant.event.history.HistoryEvents;
import com.official.pium.petPlant.event.history.LastWaterDateEvent;
import com.official.pium.history.domain.History;
import com.official.pium.history.domain.HistoryCategory;
import com.official.pium.history.domain.vo.HistoryContent;
import com.official.pium.history.domain.HistoryType;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.history.domain.vo.HistoryContent;
import com.official.pium.history.repository.HistoryCategoryRepository;
import com.official.pium.history.repository.HistoryRepository;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.petPlant.event.history.HistoryEvent;
import com.official.pium.petPlant.event.history.HistoryEvents;
import com.official.pium.petPlant.event.history.LastWaterDateEvent;
import com.official.pium.petPlant.repository.PetPlantRepository;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.official.pium.history.application;

import com.official.pium.history.application.dto.HistoryResponse;
import com.official.pium.history.domain.History;
import com.official.pium.history.domain.HistoryType;
import com.official.pium.history.repository.HistoryRepository;
import com.official.pium.member.domain.Member;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.history.repository.HistoryRepository;
import com.official.pium.petPlant.repository.PetPlantRepository;
import com.official.pium.history.application.dto.HistoryResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.official.pium.history.domain;

import com.official.pium.common.domain.BaseEntity;
import com.official.pium.petPlant.domain.PetPlant;
import com.official.pium.history.domain.vo.HistoryContent;
import com.official.pium.petPlant.domain.PetPlant;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import static org.springframework.data.domain.Sort.Direction.DESC;

import com.official.pium.member.ui.Auth;
import com.official.pium.member.domain.Member;
import com.official.pium.history.application.HistoryService;
import com.official.pium.history.application.dto.HistoryResponse;
import com.official.pium.member.domain.Member;
import com.official.pium.member.ui.Auth;
import jakarta.validation.constraints.Positive;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.official.pium.member.application;

import com.official.pium.member.domain.Member;
import com.official.pium.member.repository.MemberRepository;
import com.official.pium.member.application.dto.KaKaoAccessTokenResponse;
import com.official.pium.member.application.dto.KakaoMemberResponse;
import com.official.pium.member.application.dto.OAuthProvider;
import com.official.pium.member.domain.Member;
import com.official.pium.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Loading

0 comments on commit 3ad217b

Please sign in to comment.