Skip to content

Commit 1e7c867

Browse files
committed
[FIX] dead letter with no pk handling
1 parent 496df2f commit 1e7c867

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

backend/src/main/java/com/twtw/backend/domain/deadletter/DeadLetterConsumer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
@Component
1616
public class DeadLetterConsumer {
1717

18+
private static final String ALERT_URL = "https://hooks.slack.com/services/";
19+
private static final String ALERT_MESSAGE = "{\"text\": \"Dead letter received: %s\"}";
1820
private final WebClient webClient;
1921
private final String slackUrl;
2022

2123
public DeadLetterConsumer(@Value("${slack.url}") final String slackUrl) {
22-
this.webClient = WebClient.create("https://hooks.slack.com/services/");
24+
this.webClient = WebClient.create(ALERT_URL);
2325
this.slackUrl = slackUrl;
2426
}
2527

@@ -29,7 +31,7 @@ public void handleDeadLetterMessage(final NotificationRequest message) {
2931
webClient
3032
.post()
3133
.uri(slackUrl)
32-
.bodyValue("{\"text\": \"Dead letter received: " + message + "\"}")
34+
.bodyValue(String.format(ALERT_MESSAGE, message))
3335
.retrieve()
3436
.onStatus(HttpStatusCode::isError, ClientResponse::createException);
3537
}

backend/src/main/java/com/twtw/backend/domain/notification/messagequeue/FcmConsumer.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,45 @@
22

33
import com.google.firebase.messaging.FirebaseMessaging;
44
import com.google.firebase.messaging.FirebaseMessagingException;
5+
import com.rabbitmq.client.Channel;
56
import com.twtw.backend.domain.notification.dto.NotificationRequest;
67
import com.twtw.backend.domain.notification.entity.Notification;
78
import com.twtw.backend.domain.notification.repository.NotificationRepository;
89

10+
import lombok.RequiredArgsConstructor;
11+
912
import org.springframework.amqp.rabbit.annotation.RabbitListener;
13+
import org.springframework.amqp.support.AmqpHeaders;
14+
import org.springframework.messaging.handler.annotation.Header;
1015
import org.springframework.stereotype.Component;
1116
import org.springframework.transaction.annotation.Transactional;
1217

18+
import java.io.IOException;
19+
import java.util.Optional;
1320
import java.util.UUID;
1421

1522
@Component
23+
@RequiredArgsConstructor
1624
public class FcmConsumer {
1725
private final FirebaseMessaging firebaseMessaging;
1826
private final NotificationRepository notificationRepository;
1927

20-
public FcmConsumer(
21-
FirebaseMessaging firebaseMessaging, NotificationRepository notificationRepository) {
22-
this.firebaseMessaging = firebaseMessaging;
23-
this.notificationRepository = notificationRepository;
24-
}
25-
2628
@Transactional
2729
@RabbitListener(queues = "notification.queue")
28-
public void sendNotification(final NotificationRequest request)
29-
throws FirebaseMessagingException {
30+
public void sendNotification(
31+
final NotificationRequest request,
32+
final Channel channel,
33+
@Header(AmqpHeaders.DELIVERY_TAG) final long tag)
34+
throws FirebaseMessagingException, IOException {
3035
firebaseMessaging.send(request.toMessage());
3136

32-
notificationRepository
33-
.findById(UUID.fromString(request.getNotificationId()))
34-
.ifPresent(Notification::complete);
37+
final Optional<Notification> notification =
38+
notificationRepository.findById(UUID.fromString(request.getNotificationId()));
39+
40+
if (notification.isPresent()) {
41+
notification.get().complete();
42+
return;
43+
}
44+
channel.basicNack(tag, false, false);
3545
}
3646
}

0 commit comments

Comments
 (0)