Skip to content

Commit

Permalink
feat: event duplication processing
Browse files Browse the repository at this point in the history
  • Loading branch information
nowgnas committed Jan 9, 2024
1 parent 643c349 commit 0f8006c
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 175 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-aws-messaging:2.2.4.RELEASE'
implementation 'org.springframework.kafka:spring-kafka'
implementation 'software.amazon.awssdk:sns:2.21.37'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202401081357'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202401091420'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop
implementation 'org.springframework.boot:spring-boot-starter-aop:2.7.17'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DuplicateEventHandleAnnotation {
String eventId(); // Specify eventId for constructing the cache key
String getEventType();

String userId(); // Specify userId for constructing the cache key

long ttl() default 180; // Default TTL is 3 minutes
long getTtl() default 1L;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package kr.bb.notification.common.aop;

import bloomingblooms.domain.notification.Role;
import java.util.concurrent.TimeUnit;
import kr.bb.notification.common.annotation.DuplicateEventHandleAnnotation;
import kr.bb.notification.domain.notification.entity.NotificationCommand.NotificationInformation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
Expand All @@ -16,33 +19,36 @@
@RequiredArgsConstructor
public class DuplicateEventHandlerAop {
private final RedisTemplate<String, Object> redisTemplate;
// https://alwayspr.tistory.com/34


@Pointcut("@annotation(kr.bb.notification.common.annotation.DuplicateEventHandleAnnotation)")
public void duplicateEvent() {}

@Around("duplicateEvent()")
public Object duplicateEventHandlerAop(ProceedingJoinPoint joinPoint) throws Throwable {
String eventId = null;
Object[] args = joinPoint.getArgs();

// Check if data exists in Redis
Object cachedData = redisTemplate.opsForValue().get(eventId);

Object result = joinPoint.proceed(); // run method

if (cachedData != null) {
// Data exists in cache, return it without executing the method
return cachedData;
@Pointcut("@annotation(duplicateEventHandleAnnotation)")
public void duplicateEvent(DuplicateEventHandleAnnotation duplicateEventHandleAnnotation) {}

@Around(
value = "duplicateEvent(duplicateEventHandleAnnotation) && args(notifyData)",
argNames = "joinPoint,notifyData,duplicateEventHandleAnnotation")
public Object duplicateEventHandlerAop(
ProceedingJoinPoint joinPoint,
NotificationInformation notifyData,
DuplicateEventHandleAnnotation duplicateEventHandleAnnotation)
throws Throwable {
String eventId = notifyData.getEventId();
String id = String.valueOf(notifyData.getId());
Role role = notifyData.getRole();
String eventType = duplicateEventHandleAnnotation.getEventType();
long ttl = duplicateEventHandleAnnotation.getTtl();

String generateEventId = eventId + ":" + id + ":" + role + ":" + eventType;
Object event = redisTemplate.opsForValue().get(generateEventId);
log.info("event id : " + generateEventId);
if (event == null) {
redisTemplate.opsForValue().set(generateEventId, eventType);
redisTemplate.expire(generateEventId, ttl, TimeUnit.MINUTES); // TODO: test 후 Days로 변경

Object[] args = joinPoint.getArgs();

return joinPoint.proceed(args);
} else {
return null;
}

// Data doesn't exist in cache, proceed to execute the method
Object result = joinPoint.proceed();

// Save the result in Redis with the specified TTL
redisTemplate.opsForValue().set(key, result, duplicateEventHandlerAop.ttl());

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.bb.notification.domain.emitter.application;

import java.io.IOException;
import kr.bb.notification.common.annotation.DuplicateEventHandleAnnotation;
import kr.bb.notification.domain.emitter.repository.SSERepository;
import kr.bb.notification.domain.notification.entity.NotificationCommand.NotificationInformation;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static class NotificationInformation {
private String phoneNumber;
private Role role;
private NotificationKind notificationKind;
private String eventId;

public static List<NotificationInformation> getResaleNotificationData(
NotificationData<ResaleNotificationList> restoreNotification) {
Expand All @@ -104,6 +105,7 @@ public static List<NotificationInformation> getResaleNotificationData(
.notificationKind(
restoreNotification.getPublishInformation().getNotificationKind())
.redirectUrl(restoreNotification.getPublishInformation().getNotificationUrl())
.eventId(restoreNotification.getPublishInformation().getEventId())
.build())
.collect(Collectors.toList());
}
Expand All @@ -112,6 +114,7 @@ public static NotificationInformation getSSEData(
PublishNotificationInformation publishNotificationInformation, Long id) {
return NotificationInformation.builder()
.id(id)
.eventId(publishNotificationInformation.getEventId())
.role(publishNotificationInformation.getRole())
.notificationKind(publishNotificationInformation.getNotificationKind())
.redirectUrl(publishNotificationInformation.getNotificationUrl())
Expand All @@ -122,6 +125,7 @@ public static NotificationInformation getSSEData(
public static NotificationInformation getDeliveryNotificationData(
NotificationData<DeliveryNotification> notificationData) {
return NotificationInformation.builder()
.eventId(notificationData.getPublishInformation().getEventId())
.role(notificationData.getPublishInformation().getRole())
.phoneNumber(notificationData.getWhoToNotify().getPhoneNumber())
.type(notificationData.getWhoToNotify().getDeliveryStatus().getMessage())
Expand All @@ -134,6 +138,7 @@ public static NotificationInformation getDeliveryNotificationData(
public static NotificationInformation getSMSData(
NotificationData<InqueryResponseNotification> notification) {
return NotificationInformation.builder()
.eventId(notification.getPublishInformation().getEventId())
.notificationKind(notification.getPublishInformation().getNotificationKind())
.role(notification.getPublishInformation().getRole())
.id(notification.getWhoToNotify().getUserId())
Expand All @@ -146,6 +151,7 @@ public static NotificationInformation getSMSData(
public static NotificationInformation getNewOrderStatusData(
NotificationData<OrderStatusNotification> notification) {
return NotificationInformation.builder()
.eventId(notification.getPublishInformation().getEventId())
.notificationKind(notification.getPublishInformation().getNotificationKind())
.phoneNumber(notification.getWhoToNotify().getPhoneNumber())
.redirectUrl(notification.getPublishInformation().getNotificationUrl())
Expand Down
Loading

0 comments on commit 0f8006c

Please sign in to comment.