Skip to content

Commit

Permalink
refactor: AmqpRejectAndDontRequeueException 으로 예외변경
Browse files Browse the repository at this point in the history
- 기존엔 MemberService에서 Member를 찾았으나 NotFoundException은 DLX로 보내지못함. 일단 Amqp 예외를 던지자.

Related: #70
  • Loading branch information
hoa0217 committed Apr 3, 2024
1 parent 2b78bb9 commit bec0b40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/modoospace/alarm/service/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
import com.modoospace.common.exception.SSEConnectError;
import com.modoospace.config.redis.aspect.CachePrefixEvict;
import com.modoospace.member.domain.Member;
import com.modoospace.member.service.MemberService;
import com.modoospace.member.domain.MemberRepository;
import java.io.IOException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
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 All @@ -30,7 +30,7 @@ public class AlarmService {
private static final Long DEFAULT_TIMEOUT = 60L * 1000 * 60;
private static final String ALARM_NAME = "sse";

private final MemberService memberService;
private final MemberRepository memberRepository;
private final AlarmRepository alarmRepository;
private final AlarmQueryRepository alarmQueryRepository;
private final EmitterLocalCacheRepository emitterRepository;
Expand All @@ -54,7 +54,9 @@ public SseEmitter connectAlarm(String loginEmail) {
@Transactional
@CachePrefixEvict(cacheNames = "searchAlarms", key = "#alarmEvent.email")
public void saveAndSend(AlarmEvent alarmEvent) {
Member member = memberService.findMemberByEmail(alarmEvent.getEmail());
Member member = memberRepository.findByEmail(alarmEvent.getEmail())
.orElseThrow(() -> new AmqpRejectAndDontRequeueException(
"사용자" + 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
@@ -1,26 +1,25 @@
package com.modoospace.alarm.consumer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.modoospace.AbstractIntegrationContainerBaseTest;
import com.modoospace.alarm.controller.dto.AlarmEvent;
import com.modoospace.alarm.domain.Alarm;
import com.modoospace.alarm.domain.AlarmRepository;
import com.modoospace.alarm.domain.AlarmType;
import com.modoospace.common.exception.NotFoundEntityException;
import com.modoospace.member.domain.Member;
import com.modoospace.member.domain.MemberRepository;
import com.modoospace.member.domain.Role;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.beans.factory.annotation.Autowired;
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class AlarmConsumerTest extends AbstractIntegrationContainerBaseTest {

@Autowired
Expand Down Expand Up @@ -65,7 +64,6 @@ public void handler_ifMember_saveAlarm() throws JsonProcessingException {
.containsExactly("test facility");
}

/*
@DisplayName("Consumer는 가입되지 않은 멤버의 메시지를 소비할 경우 Exception을 던진다.")
@Test
public void handler_ifNotMember_throwException() throws JsonProcessingException {
Expand All @@ -78,7 +76,6 @@ public void handler_ifNotMember_throwException() throws JsonProcessingException
String message = objectMapper.writeValueAsString(alarmEvent);

assertThatThrownBy(() -> alarmConsumer.handler(message))
.isInstanceOf(NotFoundEntityException.class);
.isInstanceOf(AmqpRejectAndDontRequeueException.class);
}
*/
}

0 comments on commit bec0b40

Please sign in to comment.