diff --git a/backend/src/main/java/com/twtw/backend/domain/deadletter/DeadLetterConsumer.java b/backend/src/main/java/com/twtw/backend/domain/deadletter/DeadLetterConsumer.java index a59f787..8e94df9 100644 --- a/backend/src/main/java/com/twtw/backend/domain/deadletter/DeadLetterConsumer.java +++ b/backend/src/main/java/com/twtw/backend/domain/deadletter/DeadLetterConsumer.java @@ -15,11 +15,13 @@ @Component public class DeadLetterConsumer { + private static final String ALERT_URL = "https://hooks.slack.com/services/"; + private static final String ALERT_MESSAGE = "{\"text\": \"Dead letter received: %s\"}"; private final WebClient webClient; private final String slackUrl; public DeadLetterConsumer(@Value("${slack.url}") final String slackUrl) { - this.webClient = WebClient.create("https://hooks.slack.com/services/"); + this.webClient = WebClient.create(ALERT_URL); this.slackUrl = slackUrl; } @@ -29,7 +31,7 @@ public void handleDeadLetterMessage(final NotificationRequest message) { webClient .post() .uri(slackUrl) - .bodyValue("{\"text\": \"Dead letter received: " + message + "\"}") + .bodyValue(String.format(ALERT_MESSAGE, message)) .retrieve() .onStatus(HttpStatusCode::isError, ClientResponse::createException); } diff --git a/backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmConsumer.java b/backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmConsumer.java index fdbf711..21cb1f9 100644 --- a/backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmConsumer.java +++ b/backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmConsumer.java @@ -2,35 +2,45 @@ import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.FirebaseMessagingException; +import com.rabbitmq.client.Channel; import com.twtw.backend.domain.notification.dto.NotificationRequest; import com.twtw.backend.domain.notification.entity.Notification; import com.twtw.backend.domain.notification.repository.NotificationRepository; +import lombok.RequiredArgsConstructor; + import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.support.AmqpHeaders; +import org.springframework.messaging.handler.annotation.Header; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; +import java.io.IOException; +import java.util.Optional; import java.util.UUID; @Component +@RequiredArgsConstructor public class FcmConsumer { private final FirebaseMessaging firebaseMessaging; private final NotificationRepository notificationRepository; - public FcmConsumer( - FirebaseMessaging firebaseMessaging, NotificationRepository notificationRepository) { - this.firebaseMessaging = firebaseMessaging; - this.notificationRepository = notificationRepository; - } - @Transactional @RabbitListener(queues = "notification.queue") - public void sendNotification(final NotificationRequest request) - throws FirebaseMessagingException { + public void sendNotification( + final NotificationRequest request, + final Channel channel, + @Header(AmqpHeaders.DELIVERY_TAG) final long tag) + throws FirebaseMessagingException, IOException { firebaseMessaging.send(request.toMessage()); - notificationRepository - .findById(UUID.fromString(request.getNotificationId())) - .ifPresent(Notification::complete); + final Optional notification = + notificationRepository.findById(UUID.fromString(request.getNotificationId())); + + if (notification.isPresent()) { + notification.get().complete(); + return; + } + channel.basicNack(tag, false, false); } }