forked from 100-hours-a-week/10-team-joytas-dao-be
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 알림 생성 이벤트 리스너 추가 100-hours-a-week#308
- Loading branch information
1 parent
9f47571
commit b247c78
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/com/example/daobe/notification/infrastructure/sqs/SqsNotificationListener.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,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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ample/daobe/notification/infrastructure/sqs/payload/NotificationExternalEventPayload.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,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 | ||
) { | ||
} | ||
} |