Skip to content

Commit

Permalink
YEL-124 [chore] Rabbitmq 로깅 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
devkwonsehoon committed Aug 18, 2023
1 parent d9dc082 commit 901862a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
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;

@Log4j2
@Repository
@RequiredArgsConstructor
public class MessageQueueRabbitRepository implements MessageQueueRepository {
Expand All @@ -38,22 +40,26 @@ public void deleteMessageByMessageId(String messageId) throws IOException, Timeo
long deliveryTag = getDeliveryTagByMessageId(channel, messageId);

if (deliveryTag!=-1) {
channel.basicCancel(String.valueOf(deliveryTag));
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public void consumeVoteAvailableNotification(final Message message) throws IOExc
boolean exists = cooldownRepository.existsByUserId(voteAvailableQueueResponse.receiverId());
if (exists) {
notificationService.sendVoteAvailableNotification(voteAvailableQueueResponse.receiverId());
log.info("[rabbitmq] Successfully consume message %d".formatted(
message.getMessageProperties().getDeliveryTag())
);
}
} catch (Exception exception) {
log.error("[rabbitmq] %s".formatted(exception.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void produceVoteAvailableNotification(Cooldown cooldown) {
return message;
}
);
log.info("[rabbitmq] Successfully produce message. Cooldown [%s]".formatted(cooldown.getMessageId()));
} catch (Exception exception) {
log.error("[rabbitmq] %s".formatted(exception.getMessage()));
}
Expand Down

0 comments on commit 901862a

Please sign in to comment.