From 9366b605e1346a1a96a932f2d59db26d6034a0ce Mon Sep 17 00:00:00 2001 From: sungjiwoon Date: Thu, 18 Jan 2024 11:38:39 +0900 Subject: [PATCH] =?UTF-8?q?Feat=20:=20redis=20config,=20roomList=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/global/config/RedisConfig.java | 32 ++++++++++++++----- .../chat/global/pubsub/RedisSubscriber.java | 15 ++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/catchroom/chat/global/config/RedisConfig.java b/src/main/java/com/catchroom/chat/global/config/RedisConfig.java index efe955b..905eb77 100644 --- a/src/main/java/com/catchroom/chat/global/config/RedisConfig.java +++ b/src/main/java/com/catchroom/chat/global/config/RedisConfig.java @@ -44,27 +44,43 @@ public RedisConnectionFactory redisConnectionFactory() { } /** - * redis에 발행(publish)된 메시지 처리를 위한 리스너 설정 + * redis 에 발행(publish)된 메시지 처리를 위한 리스너 설정 */ @Bean - public RedisMessageListenerContainer redisMessageListener( - MessageListenerAdapter listenerAdapter, + public RedisMessageListenerContainer redisMessageListener ( + MessageListenerAdapter listenerAdapterChatMessage, ChannelTopic channelTopic ) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(redisConnectionFactory()); - container.addMessageListener(listenerAdapter, channelTopic); + container.addMessageListener(listenerAdapterChatMessage, channelTopic); return container; } - /** - * 실제 메시지를 처리하는 subscriber 설정 추가 - */ + /** 실제 메시지를 처리하는 subscriber 설정 추가*/ @Bean - public MessageListenerAdapter listenerAdapter(RedisSubscriber subscriber) { + public MessageListenerAdapter listenerAdapterChatMessage(RedisSubscriber subscriber) { return new MessageListenerAdapter(subscriber, "sendMessage"); } + @Bean + public RedisMessageListenerContainer redisMessageListenerRoomList ( + MessageListenerAdapter listenerAdapterChatRoomList, + ChannelTopic channelTopic + ) { + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(redisConnectionFactory()); + container.addMessageListener(listenerAdapterChatRoomList, channelTopic); + return container; + } + + + /** 실제 메시지 방을 처리하는 subscriber 설정 추가*/ + @Bean + public MessageListenerAdapter listenerAdapterChatRoomList(RedisSubscriber subscriber) { + return new MessageListenerAdapter(subscriber, "sendRoomList"); + } + /** * 어플리케이션에서 사용할 redisTemplate 설정 */ diff --git a/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java b/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java index 20fd8ed..7c13601 100644 --- a/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java +++ b/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java @@ -21,7 +21,7 @@ public class RedisSubscriber { */ public void sendMessage(String publishMessage) { try { - log.info("RedisSubcriber publishMsg: {}", publishMessage); + log.info("Redis Subcriber publishMsg: {}", publishMessage); // ChatMessage 객채로 맵핑 ChatMessageDto chatMessage = objectMapper.readValue(publishMessage, ChatMessageDto.class); @@ -32,5 +32,18 @@ public void sendMessage(String publishMessage) { log.error("Exception {}", e); } } + + public void sendRoomList(String message) { + try { + log.info("Redis Subcriber room publishMsg: {}", message); + // ChatMessage 객채로 맵핑 + ChatMessageDto chatMessage = objectMapper.readValue(message, ChatMessageDto.class); +// chatMessage.setMessage("메세지를 보냈습니다!"); + // 채팅방을 구독한 클라이언트에게 메시지 발송 + messagingTemplate.convertAndSend("/sub/chat/room/999cf498-bba9-43d9-9b00-d363aca2e288", chatMessage); + } catch (Exception e) { + log.error("Exception {}", e); + } + } }