-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: SQS + lambda 사용하여 Fcm 알림 구현 * sqsProperties 추가 * fix: test코드 SqsAutoConfiguration Exclude처리 * fix: FcmMessage record로 수정 및 createBatches 수정
- Loading branch information
Showing
17 changed files
with
181 additions
and
179 deletions.
There are no files selected for viewing
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
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
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
98 changes: 0 additions & 98 deletions
98
src/main/java/com/depromeet/stonebed/domain/fcm/application/FcmService.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/main/java/com/depromeet/stonebed/domain/fcm/domain/FcmMessage.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,8 @@ | ||
package com.depromeet.stonebed.domain.fcm.domain; | ||
|
||
public record FcmMessage(String title, String body, String token) { | ||
|
||
public static FcmMessage of(String title, String body, String token) { | ||
return new FcmMessage(title, body, token); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/com/depromeet/stonebed/domain/sqs/application/SqsMessageService.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,87 @@ | ||
package com.depromeet.stonebed.domain.sqs.application; | ||
|
||
import com.depromeet.stonebed.domain.fcm.domain.FcmMessage; | ||
import com.depromeet.stonebed.infra.properties.SqsProperties; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import software.amazon.awssdk.services.sqs.SqsClient; | ||
import software.amazon.awssdk.services.sqs.model.BatchResultErrorEntry; | ||
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest; | ||
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry; | ||
import software.amazon.awssdk.services.sqs.model.SendMessageBatchResponse; | ||
import software.amazon.awssdk.services.sqs.model.SendMessageRequest; | ||
import software.amazon.awssdk.services.sqs.model.SendMessageResponse; | ||
|
||
@Slf4j | ||
@Transactional | ||
@RequiredArgsConstructor | ||
@Service | ||
public class SqsMessageService { | ||
|
||
private final SqsClient sqsClient; | ||
|
||
private final SqsProperties sqsProperties; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public void sendMessage(Object message) { | ||
try { | ||
String messageBody = objectMapper.writeValueAsString(message); | ||
SendMessageRequest sendMsgRequest = | ||
SendMessageRequest.builder() | ||
.queueUrl(sqsProperties.queueUrl()) | ||
.messageBody(messageBody) | ||
.build(); | ||
|
||
SendMessageResponse sendMsgResponse = sqsClient.sendMessage(sendMsgRequest); | ||
log.info("메시지 전송 완료, ID: {}", sendMsgResponse.messageId()); | ||
} catch (Exception e) { | ||
log.error("SQS 메시지 전송 실패: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
public void sendBatchMessages(List<String> tokens, String title, String message) { | ||
List<SendMessageBatchRequestEntry> entries = new ArrayList<>(); | ||
for (String token : tokens) { | ||
try { | ||
FcmMessage fcmMessage = FcmMessage.of(title, message, token); | ||
String messageBody = objectMapper.writeValueAsString(fcmMessage); | ||
SendMessageBatchRequestEntry entry = | ||
SendMessageBatchRequestEntry.builder() | ||
.id(UUID.randomUUID().toString()) | ||
.messageBody(messageBody) | ||
.build(); | ||
entries.add(entry); | ||
} catch (Exception e) { | ||
log.error("메시지 직렬화 실패: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
SendMessageBatchRequest batchRequest = | ||
SendMessageBatchRequest.builder() | ||
.queueUrl(sqsProperties.queueUrl()) | ||
.entries(entries) | ||
.build(); | ||
|
||
try { | ||
SendMessageBatchResponse batchResponse = sqsClient.sendMessageBatch(batchRequest); | ||
|
||
// 실패한 메시지 처리 | ||
List<BatchResultErrorEntry> failedMessages = batchResponse.failed(); | ||
if (!failedMessages.isEmpty()) { | ||
for (BatchResultErrorEntry failed : failedMessages) { | ||
log.error("메시지 전송 실패, ID {}: {}", failed.id(), failed.message()); | ||
} | ||
} | ||
|
||
} catch (Exception e) { | ||
log.error("SQS 배치 메시지 전송 실패: {}", e.getMessage()); | ||
} | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
src/main/java/com/depromeet/stonebed/global/config/fcm/FcmConfig.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.