Skip to content

Commit

Permalink
Merge pull request #89 from HongDam-org/feat/rabbitmq-deadletter
Browse files Browse the repository at this point in the history
[FEAT] rabbitmq deadletter 처리
  • Loading branch information
ohksj77 authored Mar 11, 2024
2 parents 20ef0ce + a0708f6 commit d05d4fe
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twtw.backend.global.constant.RabbitMQConstant;
import com.twtw.backend.global.properties.RabbitMQProperties;

import lombok.RequiredArgsConstructor;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
Expand All @@ -29,7 +24,10 @@ public class RabbitMQConfig {

@Bean
public Queue locationQueue() {
return new Queue(RabbitMQConstant.LOCATION_QUEUE.getName(), true);
return QueueBuilder.durable(RabbitMQConstant.LOCATION_QUEUE.getName())
.withArgument("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE.getName())
.withArgument("x-dead-letter-routing-key", RabbitMQConstant.DEAD_LETTER_ROUTING_KEY.getName())
.build();
}

@Bean
Expand All @@ -46,7 +44,10 @@ public Binding locationBinding() {

@Bean
public Queue notificationQueue() {
return new Queue(RabbitMQConstant.NOTIFICATION_QUEUE.getName(), true);
return QueueBuilder.durable(RabbitMQConstant.NOTIFICATION_QUEUE.getName())
.withArgument("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE.getName())
.withArgument("x-dead-letter-routing-key", RabbitMQConstant.DEAD_LETTER_ROUTING_KEY.getName())
.build();
}

@Bean
Expand All @@ -61,6 +62,23 @@ public Binding notificationBinding() {
.with(RabbitMQConstant.NOTIFICATION_ROUTING_KEY.getName());
}

@Bean
public Queue deadLetterQueue() {
return QueueBuilder.durable(RabbitMQConstant.DEAD_LETTER_QUEUE.getName()).build();
}

@Bean
public TopicExchange deadLetterExchange() {
return new TopicExchange(RabbitMQConstant.DEAD_LETTER_EXCHANGE.getName());
}

@Bean
public Binding deadLetterBinding() {
return BindingBuilder.bind(deadLetterQueue())
.to(deadLetterExchange())
.with(RabbitMQConstant.DEAD_LETTER_ROUTING_KEY.getName());
}

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory factory = new CachingConnectionFactory();
Expand Down Expand Up @@ -94,6 +112,11 @@ public RabbitAdmin rabbitAdmin(final ConnectionFactory connectionFactory) {
rabbitAdmin.declareQueue(notificationQueue());
rabbitAdmin.declareExchange(notificationTopicExchange());
rabbitAdmin.declareBinding(notificationBinding());

rabbitAdmin.declareQueue(deadLetterQueue());
rabbitAdmin.declareExchange(deadLetterExchange());
rabbitAdmin.declareBinding(deadLetterBinding());

return rabbitAdmin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.twtw.backend.domain.deadletter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

@Slf4j
@Component
public class DeadLetterConsumer {

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.slackUrl = slackUrl;
}

@RabbitListener(queues = "deadletter.queue")
public void handleDeadLetterMessage(final String message) {
log.error("Dead letter received: {}", message);
webClient.post()
.uri(slackUrl)
.bodyValue("{\"text\": \"Dead letter received: " + message + "\"}")
.retrieve()
.bodyToMono(String.class)
.doOnSuccess(s -> log.info("Slack message sent: {}", s))
.subscribe();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ public FcmConsumer(FirebaseMessaging firebaseMessaging) {
}

@RabbitListener(queues = "notification.queue")
public void sendNotification(final NotificationRequest request) {
try {
firebaseMessaging.send(request.toMessage());
} catch (FirebaseMessagingException e) {
log.error(e.getMessage());
}
public void sendNotification(final NotificationRequest request) throws FirebaseMessagingException {
firebaseMessaging.send(request.toMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public enum RabbitMQConstant {
LOCATION_ROUTING_KEY_PREFIX("location."),
NOTIFICATION_QUEUE("notification.queue"),
NOTIFICATION_EXCHANGE("notification"),
NOTIFICATION_ROUTING_KEY("notification");
NOTIFICATION_ROUTING_KEY("notification"),
DEAD_LETTER_QUEUE("deadletter.queue"),
DEAD_LETTER_EXCHANGE("deadletter"),
DEAD_LETTER_ROUTING_KEY("deadletter");

private final String name;
}
2 changes: 2 additions & 0 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ tmap:
url: ${TMAP_URL}
firebase:
location: ${FIREBASE_LOCATION}
slack:
url: ${SLACK_URL}

0 comments on commit d05d4fe

Please sign in to comment.