Skip to content

Commit

Permalink
Merge pull request #10 from catchroom/develop
Browse files Browse the repository at this point in the history
EC2 ์‹œ๊ฐ„๋Œ€ ํ•œ๊ตญ์œผ๋กœ ๋ณ€๊ฒฝ์šฉ ๋ฐฐํฌ
  • Loading branch information
sungjiwoon authored Jan 18, 2024
2 parents ff1178f + acde7c3 commit 8df7035
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/catchroom/ChatApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.catchroom;

import jakarta.annotation.PostConstruct;
import java.util.TimeZone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class ChatApplication {
// Bean ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ด์šฉํ•œ timezone ์„ค์ •
@PostConstruct
public void started() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
}

public static void main(String[] args) {
SpringApplication.run(ChatApplication.class, args);
Expand Down
32 changes: 24 additions & 8 deletions src/main/java/com/catchroom/chat/global/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,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 ์„ค์ •
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}

0 comments on commit 8df7035

Please sign in to comment.