Skip to content

Commit

Permalink
feat: 알림 생성 이벤트 리스너 추가 100-hours-a-week#308
Browse files Browse the repository at this point in the history
  • Loading branch information
JiHongKim98 committed Nov 20, 2024
1 parent 9f47571 commit b247c78
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.daobe.notification.infrastructure.sqs;

import com.example.daobe.notification.application.NotificationCreateService;
import com.example.daobe.notification.infrastructure.sqs.payload.NotificationExternalEventPayload;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.sqs.annotation.SqsListener;
import io.awspring.cloud.sqs.listener.acknowledgement.Acknowledgement;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SqsNotificationListener {

private final ObjectMapper objectMapper;
private final NotificationCreateService notificationCreateService;

@SqsListener(value = "${aws.sqs.joytas.queue}", id = "{sqs.joytas.group-id}")
public void handleCreateNotification(String payload, Acknowledgement acknowledgement) {
NotificationExternalEventPayload eventPayload = deserialize(payload);
notificationCreateService.createNotification(eventPayload.toCommand());
acknowledgement.acknowledge();
}

private NotificationExternalEventPayload deserialize(String value) {
try {
return objectMapper.readValue(value, NotificationExternalEventPayload.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.daobe.notification.infrastructure.sqs.payload;

import com.example.daobe.notification.application.dto.command.CreateNotificationCommand;

public record NotificationExternalEventPayload(
String eventType,
Attributes attributes
) {

public CreateNotificationCommand toCommand() {
return new CreateNotificationCommand(
this.attributes.senderId(),
this.attributes.receiverId(),
this.attributes.domainId(),
this.attributes.domainName(),
this.eventType
);
}

// Nested
public record Attributes(
Long senderId,
Long receiverId,
Long domainId,
String domainName
) {
}
}

0 comments on commit b247c78

Please sign in to comment.