Skip to content

Commit

Permalink
[FEAT] rabbitmq deadletter 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Mar 11, 2024
1 parent 4e20fb9 commit 95dc70d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 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 Down Expand Up @@ -61,6 +56,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 +106,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 @@ -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 95dc70d

Please sign in to comment.