-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 알림 기능 로직 개선 및 알림 테스트 추가 (#497)
* test: 테스트용 api 수정 및 추가 * test: @EnableAsync 어노테이션 추가 * test: 알림 이벤트 로그 정보 추가 * test: 미사용 테스트 api 주석 처리 * refactor: Async ThreadExecutor 설정 변경 * refactor: 불필요한 트랜잭션 설정 제거 * refactor: Async ThreadPool 설정 수정 * refactor: Async ThreadPool 설정 30 수정 * refactor: Async ThreadPool 설정 20 수정 * refactor: Async ThreadPool 설정 40 수정 * refactor: Async ThreadPool 설정 20 수정 * refactor: Async ThreadPool 설정 30 수정 * refactor: Async ThreadPool 설정 40 수정 * refactor: Async ThreadPool 램프업 스레드 수 변경 * refactor: Async ThreadPool 스레드 수 변경 * refactor: Async ThreadPool 스레드 수 변경 * refactor: Async ThreadPool 스레드 수 변경 * refactor: Async ThreadPool 스레드 수 변경 * refactor: Async ThreadPool 스레드 수 변경 * refactor: Async ThreadPool 스레드 수 30으로 변경 * refactor: Async ThreadPool 스레드 수 50으로 변경 * refactor: Async ThreadPool 스레드 수 40으로 변경 * refactor: Async ThreadPool 스레드 수 30으로 변경 * refactor: Async ThreadPool 스레드 수 100으로 변경 * refactor: Async ThreadPool 스레드 수 8으로 변경 * refactor: Async 테스트 설정 변경 * refactor: Async 테스트 스레드 풀 설정 15로 변경 * refactor: Async 테스트 스레드 풀 설정 15로 변경 * refactor: Async 테스트 스레드 풀 설정 25로 변경 * refactor: 램프 코드 반복 횟수 변경 * refactor: 알림 허용 시 알림 발송 로직 추가 * refactor: Async thread 30으로 변경 * refactor: Async thread 35으로 변경 * refactor: Async thread 45으로 변경 * refactor: Async thread 50으로 변경 * refactor: Async thread 40으로 변경 * refactor: Async thread 50으로 변경 * test: 알림 구독 시 알림 발송 이벤트 테스트 추가 * test: 알림 전송 메서드 테스트 추가 * refactor: 알림 발송 로직 변경 * test: 알림 이벤트 리스너 테스트 추가 * chore: 테스트 컨트롤러 비활성화 * chore: 비동기 스레드 풀 코어 수 40으로 설정 * chore: 불필요한 주석 제거 * refactor: 사용하지 않는 이벤트 리스너 및 이벤트 객체 삭제
- Loading branch information
Showing
14 changed files
with
270 additions
and
65 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
backend/pium/src/main/java/com/official/pium/admin/service/TestService.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,81 @@ | ||
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; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
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(6L); | ||
// 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); | ||
} | ||
|
||
// 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(6L); | ||
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()); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
backend/pium/src/main/java/com/official/pium/admin/ui/TestController.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,31 @@ | ||
package com.official.pium.admin.ui; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/test") | ||
public class TestController { | ||
|
||
// private final TestService testService; | ||
|
||
// @GetMapping("/notifications") | ||
// public ResponseEntity<String> notificationTest() { | ||
// testService.sendWaterNotificationTest(); | ||
// return ResponseEntity.ok("알림 기능 테스트 성공"); | ||
// } | ||
|
||
// @GetMapping("/notifications/ramp") | ||
// public ResponseEntity<String> notificationRampTest() { | ||
// testService.sendWaterNotificationAsyncRampTest(); | ||
// return ResponseEntity.ok("비동기 알림 기능 테스트 램프업 성공"); | ||
// } | ||
// | ||
// @GetMapping("/notifications/async") | ||
// public ResponseEntity<String> notificationAsyncTest() { | ||
// testService.sendWaterNotificationAsyncTest(); | ||
// return ResponseEntity.ok("비동기 알림 기능 테스트 성공"); | ||
// } | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/pium/src/main/java/com/official/pium/config/AsyncConfig.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,21 @@ | ||
package com.official.pium.config; | ||
|
||
import java.util.concurrent.Executor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(40); | ||
executor.setThreadNamePrefix("2024-Pium-Thread: "); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
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
11 changes: 6 additions & 5 deletions
11
...m/src/main/java/com/official/pium/notification/application/NotificationEventListener.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 |
---|---|---|
@@ -1,23 +1,24 @@ | ||
package com.official.pium.notification.application; | ||
|
||
import com.official.pium.petPlant.event.notification.NotificationEvent; | ||
import com.official.pium.petPlant.event.notification.NotificationEvents; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class NotificationEventListener { | ||
|
||
private final NotificationService notificationService; | ||
|
||
@EventListener | ||
@Async | ||
public void handleNotificationEvents(NotificationEvents notificationEvents) { | ||
for (NotificationEvent event : notificationEvents.getNotificationEvents()) { | ||
notificationService.sendNotification(event.getDeviceToken(), event.getTitle(), event.getBody()); | ||
} | ||
public void handleNotificationEvent(NotificationEvent event) { | ||
log.info("비동기 알림 START, Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); | ||
notificationService.sendNotification(event.getDeviceToken(), event.getTitle(), event.getBody()); | ||
log.info("비동기 알림 END, Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); | ||
} | ||
} |
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
17 changes: 0 additions & 17 deletions
17
.../pium/src/main/java/com/official/pium/petPlant/event/notification/NotificationEvents.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.