Skip to content

Commit

Permalink
[FIX] dead letter with no pk handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Apr 9, 2024
1 parent 496df2f commit 1e7c867
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> notification =
notificationRepository.findById(UUID.fromString(request.getNotificationId()));

if (notification.isPresent()) {
notification.get().complete();
return;
}
channel.basicNack(tag, false, false);
}
}

0 comments on commit 1e7c867

Please sign in to comment.