-
Notifications
You must be signed in to change notification settings - Fork 5
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: 알림 기능 로직 개선 및 알림 테스트 추가 #497
The head ref may contain hidden characters: "test/490-\uC54C\uB9BC_\uD14C\uC2A4\uD2B8_\uB85C\uC9C1_\uAC1C\uC120"
Changes from 44 commits
7482a31
bea9ea6
ffe1549
4a5082f
a22ec60
47dc133
68644df
c6d8f44
8472a4a
cdcb3c2
0b3b9c8
9fc3d3d
6478813
8630a23
5604c11
9fd8651
b615b8b
597f09a
3db6b51
e28bad0
89435a4
95cc8e6
659ca56
1a13090
eb10b0c
33d1c88
9544240
81d17c3
e437712
63422eb
837d2e1
f2a1e6e
7def1fb
0db6553
c5e2efe
75d17e2
01c06a8
fcd29a3
2b1b2cf
d2defe0
3e8c068
0bee745
3724a82
9fd8bdf
a69624d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
} | ||
} |
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("비동기 알림 기능 테스트 성공"); | ||
// } | ||
} |
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: "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A디버깅할때 용이하겠군요 👍 |
||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,33 @@ | |
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 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()); | ||
} | ||
|
||
@EventListener | ||
@Async | ||
public void handleNotificationEvents(NotificationEvents notificationEvents) { | ||
log.info("동기 알림 START, Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A잘 몰라서 여쭤봅니다~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지워야 하는 메소드인데 그대로 두고 있었군요...! |
||
for (NotificationEvent event : notificationEvents.getNotificationEvents()) { | ||
notificationService.sendNotification(event.getDeviceToken(), event.getTitle(), event.getBody()); | ||
} | ||
log.info("동기 알림 END, Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
오 비동기 설정을 별도로 분리했군요! 👍