Skip to content

Commit

Permalink
Merge pull request #71 from f-lab-edu/feature/70-retry
Browse files Browse the repository at this point in the history
[#70] RabbitMQ 재처리 설정
  • Loading branch information
hoa0217 authored Apr 5, 2024
2 parents 82b3e7d + 1946ac6 commit 8d91d88
Show file tree
Hide file tree
Showing 26 changed files with 648 additions and 399 deletions.
24 changes: 12 additions & 12 deletions src/main/java/com/modoospace/alarm/consumer/AlarmConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.modoospace.alarm.controller.dto.AlarmEvent;
import com.modoospace.alarm.service.AlarmService;
import com.modoospace.common.exception.AlarmSendException;
import com.modoospace.common.exception.MessageParsingError;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
Expand All @@ -15,17 +15,17 @@
@Slf4j
public class AlarmConsumer {

private final AlarmService alarmService;
private final ObjectMapper objectMapper;
private final AlarmService alarmService;
private final ObjectMapper objectMapper;

@RabbitListener(queues = "RESERVATION")
public void handler(String message) {
try {
AlarmEvent alarmEvent = objectMapper.readValue(message, AlarmEvent.class);
alarmService.saveAndSend(alarmEvent);
log.info("Alarm save and send to client");
} catch (JsonProcessingException e) {
throw new AlarmSendException();
@RabbitListener(queues = "q.alarm.work")
public void handler(String message) {
log.info("AlarmEvent consume from q.reservation");
try {
AlarmEvent alarmEvent = objectMapper.readValue(message, AlarmEvent.class);
alarmService.saveAndSend(alarmEvent);
} catch (JsonProcessingException e) {
throw new MessageParsingError();
}
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/modoospace/alarm/consumer/AlarmDLQConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.modoospace.alarm.consumer;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
@Slf4j
public class AlarmDLQConsumer {

private static final String RETRY_COUNT_HEADER = "x-retries_count";

private final RabbitTemplate rabbitTemplate;

@Value("${spring.rabbitmq.retry_count}")
private int retryCount;

@RabbitListener(queues = "q.alarm.dead")
public void processFailedMessagesRequeue(Message failedMessage) {
Integer retriesCnt = (Integer) failedMessage.getMessageProperties().getHeaders()
.get(RETRY_COUNT_HEADER);
if (retriesCnt == null) {
retriesCnt = 0;
}
if (retriesCnt >= retryCount) {
log.info("Discarding message");
return;
}
log.info("Retrying message for the {} time", retriesCnt);
failedMessage.getMessageProperties().getHeaders().put(RETRY_COUNT_HEADER, ++retriesCnt);
rabbitTemplate.send("x.alarm.work",
failedMessage.getMessageProperties().getReceivedRoutingKey(), failedMessage);
}
}
22 changes: 11 additions & 11 deletions src/main/java/com/modoospace/alarm/producer/AlarmProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.modoospace.alarm.controller.dto.AlarmEvent;
import com.modoospace.common.exception.AlarmSendException;
import com.modoospace.common.exception.MessageParsingError;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
Expand All @@ -14,16 +14,16 @@
@Slf4j
public class AlarmProducer {

private final RabbitTemplate rabbitTemplate;
private final ObjectMapper objectMapper;
private final RabbitTemplate rabbitTemplate;
private final ObjectMapper objectMapper;

public void send(AlarmEvent alarmEvent) {
try {
String message = objectMapper.writeValueAsString(alarmEvent);
rabbitTemplate.convertAndSend("RESERVATION", message);
log.info("AlarmEvent send to RESERVATION queue");
} catch (JsonProcessingException e) {
throw new AlarmSendException();
public void send(AlarmEvent alarmEvent) {
try {
log.info("AlarmEvent produce to x.reservation");
String message = objectMapper.writeValueAsString(alarmEvent);
rabbitTemplate.convertAndSend("x.alarm.work", "", message);
} catch (JsonProcessingException e) {
throw new MessageParsingError();
}
}
}
}
6 changes: 2 additions & 4 deletions src/main/java/com/modoospace/alarm/service/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.modoospace.config.redis.aspect.CachePrefixEvict;
import com.modoospace.member.domain.Member;
import com.modoospace.member.service.MemberService;
import java.io.IOException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -19,9 +21,6 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -56,7 +55,6 @@ public SseEmitter connectAlarm(String loginEmail) {
public void saveAndSend(AlarmEvent alarmEvent) {
Member member = memberService.findMemberByEmail(alarmEvent.getEmail());
Alarm alarm = alarmRepository.save(alarmEvent.toEntity());

send(member.getEmail(), AlarmResponse.of(alarm));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.modoospace.common.exception;

public class MessageParsingError extends RuntimeException {

private static final String MESSAGE = "메세지를 파싱할 수 없습니다.";
public MessageParsingError() {
super(MESSAGE);
}
}
22 changes: 11 additions & 11 deletions src/main/java/com/modoospace/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
@Configuration
public class WebConfig implements WebMvcConfigurer {

private final LoginMemberArgumentResolver loginMemberArgumentResolver;
private final LoginMemberArgumentResolver loginMemberArgumentResolver;

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(loginMemberArgumentResolver);
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(loginMemberArgumentResolver);
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.modoospace.config.auth;

import com.modoospace.config.auth.service.CustomOAuth2UserService;
import com.modoospace.member.domain.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
Expand Down
60 changes: 0 additions & 60 deletions src/main/java/com/modoospace/config/auth/dto/AuthProvider.java

This file was deleted.

This file was deleted.

68 changes: 31 additions & 37 deletions src/main/java/com/modoospace/config/auth/dto/OAuthAttributes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.modoospace.config.auth.dto;

import com.modoospace.config.auth.provider.AuthProvider;
import com.modoospace.member.domain.Member;
import com.modoospace.member.domain.Role;
import java.util.Map;
Expand All @@ -9,45 +10,38 @@
@Getter
public class OAuthAttributes {

private Map<String, Object> attributes;
private String nameAttributeKey;
private String name;
private String email;
private Map<String, Object> attributes;
private String nameAttributeKey;
private String name;
private String email;

@Builder
public OAuthAttributes(Map<String, Object> attributes, String nameAttributeKey, String name,
String email) {
this.attributes = attributes;
this.nameAttributeKey = nameAttributeKey;
this.name = name;
this.email = email;
}
@Builder
public OAuthAttributes(Map<String, Object> attributes, String nameAttributeKey, String name,
String email) {
this.attributes = attributes;
this.nameAttributeKey = nameAttributeKey;
this.name = name;
this.email = email;
}

/**
* OAuth2User 사용자정보Map 변환
*
* @param registrationId
* @param userNameAttributeName
* @param attributes
* @return
*/
public static OAuthAttributes of(String registrationId, String userNameAttributeName,
Map<String, Object> attributes) {
AuthProvider provider = AuthProvider.findProvider(registrationId);
return provider.convert(userNameAttributeName, attributes);
}
/**
* OAuth2User 사용자정보Map 변환
*/
public static OAuthAttributes of(String registrationId, String userNameAttributeName,
Map<String, Object> attributes) {
AuthProvider provider = AuthProvider.findProvider(registrationId);
return provider.convert(userNameAttributeName, attributes);
}

/**
* UserEntity 생성 (처음 가입 시점)
*
* @return
*/
public Member toEntity() {
return Member.builder()
.name(name)
.email(email)
.role(Role.VISITOR)
.build();
}
/**
* UserEntity 생성 (처음 가입 시점)
*/
public Member toEntity() {
return Member.builder()
.name(name)
.email(email)
.role(Role.VISITOR)
.build();
}
}

Loading

0 comments on commit 8d91d88

Please sign in to comment.