RabbitMQ에서 메세지를 Consumer가 받아서 클라이언트 구독 경로로 웹소켓 전송하기 위해
Producer 객체에서 메세지를 전송할 때, Messge 객체에 Destination 헤더를 넣어 주고
Consumer 객체에서 Message객체로 받아서 헤더와 바디를 가지고 SimpleMessagingTemplate 으로 웹소켓 전송하도록 구현
sendMessage()receiveMessage()그런데 Consumer 객체에서 Message객체를 매개변수로 메세지가 받아지지 않는다.
<aside> ⚠️
Caused by: org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@4f9fc0bf}]
</aside>
byte[] 타입으로 받자receiveMessage() 메서드의 매개변수를 byte[] 로 받았더니 성공적으로 에러가 해결됨.
하지만 Destination이 없으니 메세지를 보낼 수 없다!
Message 타입과 byte[] 타입 2개를 받자.이전 버전의 RedisSubscriber 객체의 메서드에서 2개의 매개변수를 받았던 것처럼 시도해 보았습니다.
public void onMessage(Message message, byte[] pattern) {
String destination = new String(message.getChannel());
String payload = new String(message.getBody());
log.info("destination: {}, payload: {}", destination, payload);
this.messagingTemplate.convertAndSend(destination, payload);
}
하지만 실패💥
RabbitMQChatProducer
Map<String, Object>으로 변환
destination : 구독 경로message : 메세지 객체