Skip to content

Commit

Permalink
YEL-124 [merge] 충돌 방지를 위한 코드 병합
Browse files Browse the repository at this point in the history
  • Loading branch information
devkwonsehoon committed Aug 18, 2023
2 parents 92f93d9 + 161189f commit 4355e5e
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -76,7 +74,7 @@ public BaseResponse<Boolean> getYelloIdValidation(@RequestParam("yelloId") Strin
})
@PostMapping("/signup")
public BaseResponse<SignUpResponse> postSignUp(
@Valid @RequestBody SignUpRequest signUpRequest) throws IOException, TimeoutException {
@Valid @RequestBody SignUpRequest signUpRequest) {
val data = authService.signUp(signUpRequest);
return BaseResponse.success(SIGN_UP_SUCCESS, data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
import com.yello.server.global.common.manager.ConnectionManager;
import com.yello.server.infrastructure.firebase.service.NotificationService;
import com.yello.server.infrastructure.rabbitmq.repository.MessageQueueRepository;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeoutException;
import javax.validation.constraints.NotNull;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -86,7 +84,7 @@ public Boolean isYelloIdDuplicated(String yelloId) {
}

@Transactional
public SignUpResponse signUp(SignUpRequest signUpRequest) throws IOException, TimeoutException {
public SignUpResponse signUp(SignUpRequest signUpRequest) {
authManager.validateSignupRequest(signUpRequest);
final School group = schoolRepository.getById(signUpRequest.groupId());

Expand All @@ -104,7 +102,7 @@ public SignUpResponse signUp(SignUpRequest signUpRequest) throws IOException, Ti
}

@Transactional
public void recommendUser(String recommendYelloId, String userYelloId) throws IOException, TimeoutException {
public void recommendUser(String recommendYelloId, String userYelloId) {
if (recommendYelloId!=null && !recommendYelloId.isEmpty()) {
User recommendedUser = userRepository.getByYelloId(recommendYelloId);
User user = userRepository.getByYelloId(userYelloId);
Expand All @@ -117,11 +115,7 @@ public void recommendUser(String recommendYelloId, String userYelloId) throws IO

final Optional<Cooldown> cooldown =
cooldownRepository.findByUserId(recommendedUser.getId());

if (cooldown.isPresent()) {
cooldownRepository.delete(cooldown.get());
messageQueueRepository.deleteMessageByMessageId(cooldown.get().getMessageId());
}
cooldown.ifPresent(cooldownRepository::delete);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public interface CooldownJpaRepository extends JpaRepository<Cooldown, Long> {
"where c.user.id = :userId " +
"and c.deletedAt is null")
boolean existsByUserId(Long userId);

@Query("select case when count(c) > 0 then true else false end from Cooldown c " +
"where c.messageId = :messageId " +
"and c.deletedAt is null")
boolean existsByMessageId(String messageId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface CooldownRepository {

boolean existsByUserId(Long userId);

boolean existsByMessageId(String messageId);

Optional<Cooldown> findByUserIdNotFiltered(Long userId);

void delete(Cooldown cooldown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public boolean existsByUserId(Long userId) {
return cooldownJpaRepository.existsByUserId(userId);
}

@Override
public boolean existsByMessageId(String messageId) {
return cooldownJpaRepository.existsByMessageId(messageId);
}

@Override
public Optional<Cooldown> findByUserIdNotFiltered(Long userId) {
return cooldownJpaRepository.findByUserIdNotFiltered(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public Message createMessage(String deviceToken, NotificationMessage notificatio
return Message.builder()
.setToken(deviceToken)
.setNotification(notificationMessage.toNotification())
.putData("title", notificationMessage.title())
.putData("body", notificationMessage.message())
.putData("type", notificationMessage.type().name())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.yello.server.infrastructure.rabbitmq.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.GetResponse;
import com.yello.server.infrastructure.rabbitmq.dto.response.VoteAvailableQueueResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Repository;

Expand All @@ -30,42 +22,4 @@ public void convertAndSend(
) {
rabbitTemplate.convertAndSend(exchange, routingKey, message, messagePostProcessor);
}

@Override
public void deleteMessageByMessageId(String messageId) throws IOException, TimeoutException {
Connection connection = rabbitTemplate.getConnectionFactory()
.createConnection();

try (Channel channel = connection.createChannel(true)) {
long deliveryTag = getDeliveryTagByMessageId(channel, messageId);

if (deliveryTag!=-1) {
channel.basicAck(deliveryTag, false);
log.info("[rabbitmq] Successfully delete message %d !".formatted(deliveryTag));
}
}
}

private long getDeliveryTagByMessageId(Channel channel, String messageId) throws IOException {
GetResponse response = channel.basicGet("vote-available-notification-queue", false);
log.info("[rabbitmq] Successfully get channel %s".formatted(response.getEnvelope().getExchange()));

while (response!=null) {
log.info("[rabbitmq] Searching message for remove ...");
String messageBody = new String(response.getBody(), StandardCharsets.UTF_8);
ObjectMapper objectMapper = new ObjectMapper();

VoteAvailableQueueResponse voteAvailableQueueResponse = objectMapper.readValue(messageBody,
VoteAvailableQueueResponse.class);

if (voteAvailableQueueResponse.messageId().equals(messageId)) {
log.info("[rabbitmq] find message: %d".formatted(response.getEnvelope().getDeliveryTag()));
return response.getEnvelope().getDeliveryTag();
}

response = channel.basicGet("vote-available-notification-queue", false);
}

return -1;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.yello.server.infrastructure.rabbitmq.repository;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.springframework.amqp.core.MessagePostProcessor;

public interface MessageQueueRepository {

void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor);

void deleteMessageByMessageId(String messageId) throws IOException, TimeoutException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ConsumerRabbitmqService implements ConsumerService {
@Override
@RabbitListener(queues = "vote-available-notification-queue", concurrency = "6")
public void consumeVoteAvailableNotification(final Message message) throws IOException {
log.info("[rabbitmq] start consume message. [%s]".formatted(message.getMessageProperties().getMessageId()));
ObjectMapper objectMapper = new ObjectMapper();

final VoteAvailableQueueResponse voteAvailableQueueResponse = objectMapper.readValue(
Expand All @@ -31,11 +32,15 @@ public void consumeVoteAvailableNotification(final Message message) throws IOExc
);

try {
boolean exists = cooldownRepository.existsByUserId(voteAvailableQueueResponse.receiverId());
boolean exists = cooldownRepository.existsByMessageId(voteAvailableQueueResponse.messageId());
if (exists) {
notificationService.sendVoteAvailableNotification(voteAvailableQueueResponse.receiverId());
log.info("[rabbitmq] Successfully consume message %d".formatted(
message.getMessageProperties().getDeliveryTag())
log.info("[rabbitmq] Successfully consume message %s".formatted(
voteAvailableQueueResponse.messageId())
);
} else {
log.info("[rabbitmq] Already removed message %s".formatted(
voteAvailableQueueResponse.messageId())
);
}
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public boolean existsByUserId(Long userId) {
return false;
}

@Override
public boolean existsByMessageId(String messageId) {
return false;
}

@Override
public Optional<Cooldown> findByUserIdNotFiltered(Long userId) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.yello.server.small.global.rabbitmq;

import com.yello.server.infrastructure.rabbitmq.repository.MessageQueueRepository;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.springframework.amqp.core.MessagePostProcessor;

public class FakeMessageQueueRepository implements MessageQueueRepository {
Expand All @@ -12,9 +10,4 @@ public void convertAndSend(String exchange, String routingKey, Object message,
MessagePostProcessor messagePostProcessor) {

}

@Override
public void deleteMessageByMessageId(String messageId) throws IOException, TimeoutException {
System.out.println("Delete message by message id " + messageId);
}
}

0 comments on commit 4355e5e

Please sign in to comment.