-
Notifications
You must be signed in to change notification settings - Fork 0
UPLUS-134 알림 이벤트 Consumer 기능 구현 (초안) #2
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
Changes from all commits
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,21 @@ | ||
| package com.project.notification.consumer; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| import org.springframework.data.redis.core.StringRedisTemplate; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class NotificationSendDedupService { | ||
| private final StringRedisTemplate redisTemplate; | ||
| private static final Duration TTL = Duration.ofDays(7); | ||
|
|
||
| public boolean tryAcquire(String eventId) { | ||
| Boolean success = | ||
| redisTemplate.opsForValue().setIfAbsent("notification:event:" + eventId, "1", TTL); | ||
|
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. Redis 키의 접두사 예시: private static final String DEDUP_KEY_PREFIX = "notification:event:";
private static final String DEDUP_KEY_VALUE = "1";
// ...
redisTemplate.opsForValue().setIfAbsent(DEDUP_KEY_PREFIX + eventId, DEDUP_KEY_VALUE, TTL); |
||
| return Boolean.TRUE.equals(success); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.project.notification.consumer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| public class SendNotificationLogger { | ||
|
|
||
| private static final Path LOG_PATH = Path.of("logs/notification-preview.log"); | ||
|
|
||
| public static void write(String content) { | ||
| try { | ||
| Files.createDirectories(LOG_PATH.getParent()); | ||
|
|
||
| Files.writeString( | ||
| LOG_PATH, | ||
| """ | ||
| =============================== | ||
| %s | ||
| %s | ||
| =============================== | ||
|
|
||
| """ | ||
| .formatted(LocalDateTime.now(), content), | ||
| StandardOpenOption.CREATE, | ||
| StandardOpenOption.WRITE, | ||
| StandardOpenOption.APPEND); | ||
| } catch (IOException e) { | ||
| log.error("Failed to write notification preview file", e); | ||
| } | ||
| } | ||
|
Comment on lines
+14
to
+36
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.
운영 환경을 고려하여, 이 클래스를 Spring Bean으로 만들고 Logback/Log4j2 같은 로깅 프레임워크의 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.project.notification.consumer; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public record UsageNotificationEvent( | ||
| UUID eventId, | ||
| Long id, | ||
| Long subId, | ||
| String period, | ||
| String unit, | ||
| int threshold, | ||
| int percent, | ||
| long totalUsedMb, | ||
| long allotmentMb) {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.project.notification.consumer; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class UsageNotificationMessageFormatter { | ||
|
|
||
| public String format(UsageNotificationEvent event, LocalDateTime now) { | ||
|
|
||
| String providedGb = formatGb(event.allotmentMb()); | ||
| String usedGb = formatGb(event.totalUsedMb()); | ||
| String time = now.format(DateTimeFormatter.ofPattern("MM/dd HH:mm:ss")); | ||
|
|
||
| return """ | ||
| [LG U+] | ||
| [Web발신] | ||
| [LG U+] 이번 달 데이터 사용량 안내 | ||
|
|
||
| 고객님, 「유쓰 5G 데이터 플러스」 | ||
| 요금제의 기본 데이터 사용량을 안내해 드립니다. | ||
|
|
||
| ▶ 데이터 사용량 안내 | ||
| - 제공량: %sGB | ||
| - 사용량: %d%% %sGB | ||
| ※ %s 기준 | ||
|
|
||
| """ | ||
| .formatted(providedGb, event.percent(), usedGb, time); | ||
| } | ||
|
|
||
| private String formatGb(long mb) { | ||
| return String.format("%.2f", mb / 1024.0); | ||
|
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. |
||
| } | ||
| } | ||
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.
예외 발생 시 메시지를
ack처리하면 해당 메시지는 유실되어 알림이 누락될 수 있습니다. 이는 데이터 정합성에 심각한 문제를 야기할 수 있습니다.일시적인 오류(예: DB 접속 불가)의 경우 재시도를 통해 문제를 해결할 수 있어야 하며, 영구적인 오류(예: 메시지 포맷 오류)의 경우 메시지를 Dead Letter Queue(DLQ)로 보내 나중에 분석할 수 있도록 하는 것이 좋습니다.
ack.acknowledge()를 호출하는 대신, 예외를 다시 던지거나SeekToCurrentErrorHandler와 같은 스프링 카프카의 오류 처리 메커니즘을 활용하여 재시도 및 DLQ 전송을 구현하는 것을 강력히 권장합니다.