RabbitMQ의 메세지 브로커에서는 발행을 exchange와 routingKey로 하다보니
Consumer가 발행한 메세지를 소비할 때, 웹소켓 전송할 구독 경로를 바로 알지 못하는 문제가 발생하였습니다.
destination 키로 구독 경로 넣어주기RabbitMQMessageService에서 메세지 발행 시, Message 객체의 Header에
destination키로 구독 경로 넣어주기
private Message getMessage(String destination, Object meesageObject) {
try {
byte[] messageBody = objectMapper.writeValueAsBytes(meesageObject);
MessageProperties properties = new MessageProperties();
properties.setHeader("destination", destination);
properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
return MessageBuilder.withBody(messageBody)
.andProperties(properties)
.build();
} catch (JsonProcessingException e) {
throw new ApiException(ErrorStatus.JSON_CHANGE_FAILED);
}
}
성공!👍
하지만, 계속 RabbitMQ에 대해 알아보다보니 Message객체에서 메세지를 발행한 QueueName을 가져올 수 있다는 것을 알게되었습니다.
따라서
chat-room/{roomId}chat-room/{roomId}/initonMessage() 메서드에서 받은 Message 객체에서 큐 이름 가져오기replace() 함수를 사용하여 큐 이름에서 . 을 / 로 변경/topic/ prefix를 destination 경로에 추가init 키워드 포함 여부로 이전 메세지 불러오기용 큐로 발행을 판단
userId 키로 메세지를 구독할 유저 ID를 추가/{userId} 를 추가SimpMessagingTemplate 으로 웹소켓 전송 시, 위에서 만든 destination 으로 메세지 전송