From 4e4276a78c7d3e858770bb61a9ae2dec3c4e0d02 Mon Sep 17 00:00:00 2001 From: de123456sdf Date: Sun, 30 Jun 2024 04:54:02 +0900 Subject: [PATCH 1/2] =?UTF-8?q?delete:=20=EB=A0=88=EB=94=94=EC=8A=A4=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/service/ChatRoomNotification.java | 11 ---- .../domain/chat/service/MessagePublisher.java | 21 ------- .../domain/chat/service/RedisSubscriber.java | 35 ----------- .../global/config/RedisConfig.java | 63 ------------------- 4 files changed, 130 deletions(-) delete mode 100644 src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatRoomNotification.java delete mode 100644 src/main/java/team9502/sinchulgwinong/domain/chat/service/MessagePublisher.java delete mode 100644 src/main/java/team9502/sinchulgwinong/domain/chat/service/RedisSubscriber.java delete mode 100644 src/main/java/team9502/sinchulgwinong/global/config/RedisConfig.java diff --git a/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatRoomNotification.java b/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatRoomNotification.java deleted file mode 100644 index d992c15..0000000 --- a/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatRoomNotification.java +++ /dev/null @@ -1,11 +0,0 @@ -package team9502.sinchulgwinong.domain.chat.service; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class ChatRoomNotification { - private Long chatRoomId; - private String message; -} diff --git a/src/main/java/team9502/sinchulgwinong/domain/chat/service/MessagePublisher.java b/src/main/java/team9502/sinchulgwinong/domain/chat/service/MessagePublisher.java deleted file mode 100644 index 8d60eee..0000000 --- a/src/main/java/team9502/sinchulgwinong/domain/chat/service/MessagePublisher.java +++ /dev/null @@ -1,21 +0,0 @@ -package team9502.sinchulgwinong.domain.chat.service; - - -import lombok.RequiredArgsConstructor; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.listener.ChannelTopic; -import org.springframework.stereotype.Component; -import team9502.sinchulgwinong.domain.chat.entity.ChatMessage; - -@Component -@RequiredArgsConstructor -public class MessagePublisher { - - private final RedisTemplate redisTemplate; - private final ChannelTopic topic; - - public void publish(ChatMessage message) { - - redisTemplate.convertAndSend(topic.getTopic(), message); - } -} diff --git a/src/main/java/team9502/sinchulgwinong/domain/chat/service/RedisSubscriber.java b/src/main/java/team9502/sinchulgwinong/domain/chat/service/RedisSubscriber.java deleted file mode 100644 index d6f2d48..0000000 --- a/src/main/java/team9502/sinchulgwinong/domain/chat/service/RedisSubscriber.java +++ /dev/null @@ -1,35 +0,0 @@ -package team9502.sinchulgwinong.domain.chat.service; - - -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.RequiredArgsConstructor; -import org.springframework.data.redis.connection.Message; -import org.springframework.data.redis.connection.MessageListener; -import org.springframework.messaging.simp.SimpMessagingTemplate; -import org.springframework.stereotype.Service; -import team9502.sinchulgwinong.domain.chat.entity.ChatMessage; - -@Service -@RequiredArgsConstructor -public class RedisSubscriber implements MessageListener { - - private final SimpMessagingTemplate messagingTemplate; - private final ObjectMapper objectMapper; - - @Override - public void onMessage(Message message, byte[] pattern) { - try { - ChatMessage chatMessage = objectMapper.readValue(message.getBody(), ChatMessage.class); - - messagingTemplate.convertAndSend("/topic/chatroom/" + - chatMessage.getChatRoom().getChatRoomId(), chatMessage); - - // 채팅방 목록 갱신 및 새로운 메시지 알림 - messagingTemplate.convertAndSend("/topic/chatrooms", - new ChatRoomNotification(chatMessage.getChatRoom().getChatRoomId(), "New message")); - - } catch (Exception e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/src/main/java/team9502/sinchulgwinong/global/config/RedisConfig.java b/src/main/java/team9502/sinchulgwinong/global/config/RedisConfig.java deleted file mode 100644 index 0ef0d3c..0000000 --- a/src/main/java/team9502/sinchulgwinong/global/config/RedisConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package team9502.sinchulgwinong.global.config; - -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.RequiredArgsConstructor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.listener.ChannelTopic; -import org.springframework.data.redis.listener.RedisMessageListenerContainer; -import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; -import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; -import org.springframework.data.redis.serializer.StringRedisSerializer; -import team9502.sinchulgwinong.domain.chat.service.RedisSubscriber; - -@Configuration -@RequiredArgsConstructor -public class RedisConfig { - - private final ObjectMapper objectMapper; - - @Bean - public RedisConnectionFactory redisConnectionFactory() { - return new LettuceConnectionFactory(); - } - - @Bean - public RedisTemplate redisTemplate() { - - RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(redisConnectionFactory()); - template.setKeySerializer(new StringRedisSerializer()); - template.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)); - - return template; - } - - @Bean - public RedisMessageListenerContainer redisContainer( - RedisConnectionFactory connectionFactory, - MessageListenerAdapter listenerAdapter, - ChannelTopic topic) { - - RedisMessageListenerContainer container = new RedisMessageListenerContainer(); - container.setConnectionFactory(connectionFactory); - container.addMessageListener(listenerAdapter, topic); - - return container; - } - - @Bean - public ChannelTopic topic() { - - return new ChannelTopic("chatroom"); - } - - @Bean - public MessageListenerAdapter messageListener(RedisSubscriber subscriber) { - - return new MessageListenerAdapter(subscriber, "onMessage"); - } -} From a90e4f8d0343346d535648c69a8d9a3e45fb7e50 Mon Sep 17 00:00:00 2001 From: de123456sdf Date: Sun, 30 Jun 2024 04:56:38 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20Message=20=EB=A7=A4=ED=95=91?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95,=20=EC=9B=B9=EC=86=8C?= =?UTF-8?q?=EC=BC=93=EC=9C=BC=EB=A1=9C=20=EC=8B=A4=EC=8B=9C=EA=B0=84=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatController.java | 25 +++++-------- .../dto/response/ChatMessageResponseDTO.java | 10 +++--- .../domain/chat/service/ChatService.java | 35 +++++++++++-------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/main/java/team9502/sinchulgwinong/domain/chat/controller/ChatController.java b/src/main/java/team9502/sinchulgwinong/domain/chat/controller/ChatController.java index 1cf52cc..08c1088 100644 --- a/src/main/java/team9502/sinchulgwinong/domain/chat/controller/ChatController.java +++ b/src/main/java/team9502/sinchulgwinong/domain/chat/controller/ChatController.java @@ -3,6 +3,8 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import team9502.sinchulgwinong.domain.chat.dto.request.ChatRequestDTO; @@ -18,13 +20,12 @@ import static team9502.sinchulgwinong.global.response.SuccessCode.*; @RestController -@RequestMapping("/chats") @RequiredArgsConstructor public class ChatController { private final ChatService chatService; - @PostMapping("/cp-user/{cpUserId}") + @PostMapping("/chats/cp-user/{cpUserId}") public ResponseEntity> createChatRoom( @AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("cpUserId") Long cpUserId) { @@ -42,7 +43,7 @@ public ResponseEntity> createChatRoom( ); } - @GetMapping("/chat-rooms") + @GetMapping("/chats/chat-rooms") public ResponseEntity>> getChatRooms( @AuthenticationPrincipal UserDetailsImpl userDetails) { @@ -56,25 +57,17 @@ public ResponseEntity>> getChatRooms ); } - @PostMapping("/chat-room/{chatRoomId}") - public ResponseEntity> sendMessage( + @MessageMapping("/chats/chat-room/{chatRoomId}") + @SendTo("/topic/chatroom/{chatRoomId}") + public ChatMessageResponseDTO saveAndSendMessage( @RequestBody @Valid ChatRequestDTO chatRequestDTO, @PathVariable(name = "chatRoomId") Long chatRoomId, @AuthenticationPrincipal UserDetailsImpl userDetails) { - ChatMessageResponseDTO chatMessageResponseDTO = - chatService.saveAndPublishMessage(userDetails, chatRequestDTO, chatRoomId); - - return ResponseEntity.status(SUCCESS_SEND_AND_SAVE_CHAT.getHttpStatus()) - .body( - GlobalApiResponse.of( - SUCCESS_SEND_AND_SAVE_CHAT.getMessage(), - chatMessageResponseDTO - ) - ); + return chatService.saveAndSendMessage(userDetails, chatRequestDTO, chatRoomId); } - @GetMapping("/chat-room/{chatRoomId}") + @GetMapping("/chats/chat-room/{chatRoomId}") public ResponseEntity>> getChatMessages( @PathVariable(name = "chatRoomId") Long chatRoomId) { diff --git a/src/main/java/team9502/sinchulgwinong/domain/chat/dto/response/ChatMessageResponseDTO.java b/src/main/java/team9502/sinchulgwinong/domain/chat/dto/response/ChatMessageResponseDTO.java index ff5f51c..51285e2 100644 --- a/src/main/java/team9502/sinchulgwinong/domain/chat/dto/response/ChatMessageResponseDTO.java +++ b/src/main/java/team9502/sinchulgwinong/domain/chat/dto/response/ChatMessageResponseDTO.java @@ -26,17 +26,15 @@ public ChatMessageResponseDTO(ChatMessage chatMessage) { this.message = chatMessage.getMessage(); this.createAt = chatMessage.getCreatedAt(); - if(chatMessage.getCompanyUser() != null){ + if (chatMessage.getCompanyUser() != null) { this.cpUserId = chatMessage.getCompanyUser().getCpUserId(); - } - else{ + } else { this.cpUserId = null; } - if (chatMessage.getUser() != null){ + if (chatMessage.getUser() != null) { this.userId = chatMessage.getUser().getUserId(); - } - else{ + } else { this.userId = null; } } diff --git a/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatService.java b/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatService.java index f605146..df4b096 100644 --- a/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatService.java +++ b/src/main/java/team9502/sinchulgwinong/domain/chat/service/ChatService.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; +import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import team9502.sinchulgwinong.domain.chat.dto.request.ChatRequestDTO; @@ -21,6 +22,7 @@ import java.util.List; import java.util.stream.Collectors; + @Service @RequiredArgsConstructor public class ChatService { @@ -28,7 +30,7 @@ public class ChatService { private final ChatMessageRepository chatMessageRepository; private final CompanyUserRepository companyUserRepository; private final ChatRoomRepository chatRoomRepository; - private final MessagePublisher messagePublisher; + private final SimpMessagingTemplate messagingTemplate; @Transactional public ChatRoomResponseDTO createChatRoom(User user, Long cpUserId) { @@ -44,6 +46,8 @@ public ChatRoomResponseDTO createChatRoom(User user, Long cpUserId) { chatRoomRepository.save(chatRoom); + notifyChatRoomUpdate(); + return new ChatRoomResponseDTO(chatRoom); } @@ -62,12 +66,17 @@ public List getChatRooms(UserDetailsImpl userDetails) { .collect(Collectors.toList()); } - @Transactional - public ChatMessageResponseDTO saveAndPublishMessage( - UserDetailsImpl userDetails, - ChatRequestDTO chatRequestDTO, - Long chatRoomId) { + public void notifyChatRoomUpdate() { + List chatRooms = chatRoomRepository.findAll() + .stream() + .map(ChatRoomResponseDTO::new) + .collect(Collectors.toList()); + + messagingTemplate.convertAndSend("/topic/chatrooms", chatRooms); + } + @Transactional + public ChatMessageResponseDTO saveAndSendMessage(UserDetailsImpl userDetails, ChatRequestDTO chatRequestDTO, Long chatRoomId) { ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId) .orElseThrow(() -> new ApiException(ErrorCode.CHAT_NOT_FOUND)); @@ -76,38 +85,36 @@ public ChatMessageResponseDTO saveAndPublishMessage( chatMessage.setChatRoom(chatRoom); if (userDetails.getUser() != null) { - User user = (User) userDetails.getUser(); chatMessage.setUser(user); chatMessage.setCompanyUser(null); - } else if (userDetails.getCpUserId() != null) { - CompanyUser companyUser = companyUserRepository.findById(userDetails.getCpUserId()) .orElseThrow(() -> new ApiException(ErrorCode.COMPANY_USER_NOT_FOUND)); - chatMessage.setCompanyUser(companyUser); chatMessage.setUser(null); } chatMessageRepository.save(chatMessage); - messagePublisher.publish(chatMessage); chatRoom.setChatCheck(true); chatRoomRepository.save(chatRoom); + messagingTemplate.convertAndSend("/topic/chatroom/" + chatRoomId, new ChatMessageResponseDTO(chatMessage)); + + notifyChatRoomUpdate(); + return new ChatMessageResponseDTO(chatMessage); } @Transactional public List getChatMessages(Long chatRoomId) { - ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId) - .orElseThrow(()-> new ApiException(ErrorCode.CHAT_NOT_FOUND)); + .orElseThrow(() -> new ApiException(ErrorCode.CHAT_NOT_FOUND)); List messages = chatMessageRepository.findByChatRoom_ChatRoomId(chatRoomId); - if(chatRoom.isChatCheck()){ + if (chatRoom.isChatCheck()) { chatRoom.setChatCheck(false); }