-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Chat): 채팅 메시지 발송 Redis Pub/Sub 구현
- Loading branch information
1 parent
2ca8b89
commit e23df26
Showing
9 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 21 additions & 7 deletions
28
application/src/main/kotlin/com/threedays/application/chat/service/ReceiveMessageService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,32 @@ | ||
package com.threedays.application.chat.service | ||
|
||
import com.threedays.application.chat.port.inbound.ReceiveMessage | ||
import com.threedays.domain.chat.entity.Message | ||
import com.threedays.domain.chat.repository.MessageRepository | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ReceiveMessageService( | ||
private val messageRepository: MessageRepository, | ||
) : ReceiveMessage { | ||
class ReceiveMessageService() : ReceiveMessage { | ||
|
||
override fun invoke(command: Message) { | ||
messageRepository.save(command) | ||
companion object { | ||
|
||
private val logger = KotlinLogging.logger { } | ||
} | ||
|
||
private val scope = CoroutineScope(Dispatchers.IO) | ||
|
||
override fun invoke(command: ReceiveMessage.Command) { | ||
scope.launch(exceptionHandler) { | ||
logger.info { "Received message: $command" } | ||
// TODO Implement message processing | ||
} | ||
} | ||
|
||
private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
logger.error(throwable) { "An error occurred while processing the message" } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
infrastructure/redis/src/main/kotlin/com/threedays/redis/chat/MessageProducerRedisAdapter.kt
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
...ure/redis/src/main/kotlin/com/threedays/redis/chat/adapter/MessageProducerRedisAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.threedays.redis.chat.adapter | ||
|
||
import com.threedays.application.chat.port.outbound.MessageProducer | ||
import com.threedays.domain.chat.entity.Message | ||
import com.threedays.redis.chat.event.MessageRedisEvent | ||
import com.threedays.redis.chat.event.MessageRedisEvent.Sent.Companion.toSentEvent | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class MessageProducerRedisAdapter( | ||
private val redisTemplate: RedisTemplate<String, Any> | ||
) : MessageProducer { | ||
|
||
companion object { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
} | ||
|
||
override fun produceSendEvent(message: Message) { | ||
logger.debug { "Producing message sent event to redis: $message" } | ||
|
||
val messageSentEvent: MessageRedisEvent.Sent = message.toSentEvent() | ||
redisTemplate.convertAndSend(messageSentEvent.getPartitionKey(), messageSentEvent) | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...e/redis/src/main/kotlin/com/threedays/redis/chat/adapter/MessageSubscriberRedisAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.threedays.redis.chat.adapter | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.threedays.application.chat.port.inbound.ReceiveMessage | ||
import com.threedays.redis.chat.event.MessageRedisEvent | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.data.redis.connection.Message | ||
import org.springframework.data.redis.connection.MessageListener | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class MessageSubscriberRedisAdapter( | ||
private val receiveMessage: ReceiveMessage, | ||
private val objectMapper: ObjectMapper, | ||
) : MessageListener { | ||
|
||
companion object { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
} | ||
|
||
|
||
override fun onMessage( | ||
message: Message, | ||
pattern: ByteArray? | ||
) { | ||
try { | ||
val channel = String(message.channel) | ||
val body = String(message.body) | ||
logger.debug { "Received message from channel '$channel': $body" } | ||
|
||
val event: MessageRedisEvent.Sent = objectMapper.readValue(body, MessageRedisEvent.Sent::class.java) | ||
handleEvent(event) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Failed to process Redis message" } | ||
} | ||
} | ||
|
||
private fun handleEvent(event: MessageRedisEvent.Sent) { | ||
logger.info { "Handling received event: $event" } | ||
val command = ReceiveMessage.Command(event.message) | ||
receiveMessage.invoke(command) | ||
} | ||
|
||
|
||
} |
17 changes: 17 additions & 0 deletions
17
infrastructure/redis/src/main/kotlin/com/threedays/redis/chat/event/MessageRedisEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.threedays.redis.chat.event | ||
|
||
import com.threedays.domain.chat.entity.Message | ||
|
||
sealed class MessageRedisEvent { | ||
|
||
data class Sent(val message: Message) { | ||
companion object { | ||
fun Message.toSentEvent() = Sent(this) | ||
} | ||
|
||
fun getPartitionKey() = message.getPartitionKey() | ||
} | ||
|
||
|
||
|
||
} |