-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Redis PubSub * Redis PubSub * Redis PubSub - review comments incorporated * Redis PubSub - review comments incorporated * Redis PubSub - review comments incorporated * Redis PubSub - review comments incorporated * Redis PubSub - review comments incorporated
- Loading branch information
Showing
7 changed files
with
118 additions
and
0 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
3 changes: 3 additions & 0 deletions
3
kotlin-libraries-data/src/main/kotlin/com/baeldung/redispubsub/Message.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,3 @@ | ||
package com.baeldung.redispubsub | ||
|
||
data class Message(val content: String) |
17 changes: 17 additions & 0 deletions
17
kotlin-libraries-data/src/main/kotlin/com/baeldung/redispubsub/MessageListener.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.baeldung.redispubsub | ||
|
||
import io.lettuce.core.pubsub.RedisPubSubAdapter | ||
import java.util.concurrent.CountDownLatch | ||
|
||
class MessageListener : RedisPubSubAdapter<String, String>() { | ||
|
||
var latch: CountDownLatch = CountDownLatch(1) | ||
|
||
var messagesReceived: List<String> = emptyList() | ||
override fun message(channel: String?, message: String?) { | ||
println("Received message: $message from channel: $channel") | ||
messagesReceived = messagesReceived.plus(message!!) | ||
latch.countDown() | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
kotlin-libraries-data/src/main/kotlin/com/baeldung/redispubsub/RedisConnectionManager.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,26 @@ | ||
package com.baeldung.redispubsub | ||
|
||
import io.lettuce.core.RedisClient | ||
import io.lettuce.core.api.StatefulRedisConnection | ||
import io.lettuce.core.api.sync.RedisCommands | ||
import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands | ||
|
||
object RedisConnectionManager: AutoCloseable { | ||
private val redisClient: RedisClient = RedisClient.create("redis://localhost:6379") | ||
private val connection: StatefulRedisConnection<String, String> = redisClient.connect() | ||
|
||
override fun close() { | ||
connection.close() | ||
redisClient.shutdown() | ||
} | ||
|
||
fun redisSyncCommands(): RedisCommands<String, String>? { | ||
return connection.sync() | ||
} | ||
|
||
fun redisPubSubAsyncCommands(messageListener: MessageListener): RedisPubSubAsyncCommands<String, String> { | ||
val pubSubConnection = redisClient.connectPubSub() | ||
pubSubConnection.addListener(messageListener) | ||
return pubSubConnection.async() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
kotlin-libraries-data/src/main/kotlin/com/baeldung/redispubsub/RedisPublisher.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,9 @@ | ||
package com.baeldung.redispubsub | ||
|
||
class RedisPublisher { | ||
|
||
fun publishMessage(channel: String, message: Message) { | ||
RedisConnectionManager.redisSyncCommands()?.publish(channel, message.content) | ||
println("Message published: $message") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
kotlin-libraries-data/src/main/kotlin/com/baeldung/redispubsub/RedisSubscriber.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,9 @@ | ||
package com.baeldung.redispubsub | ||
|
||
class RedisSubscriber(private val messageListener: MessageListener) { | ||
|
||
fun subscribeToChannel(channel: String) { | ||
RedisConnectionManager.redisPubSubAsyncCommands(messageListener).subscribe(channel) | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
kotlin-libraries-data/src/test/kotlin/com/baeldung/redispubsub/RedisSubscriberUnitTest.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,41 @@ | ||
package com.baeldung.redispubsub | ||
|
||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import redis.embedded.RedisServer | ||
import java.util.concurrent.TimeUnit | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class RedisSubscriberUnitTest { | ||
|
||
val messageListener = MessageListener() | ||
val redisSubscriber = RedisSubscriber(messageListener) | ||
val redisPublisher = RedisPublisher() | ||
val channel = "channel" | ||
val message = Message("Hello, Redis!") | ||
|
||
val redisServer = RedisServer(6379) | ||
|
||
@BeforeAll | ||
fun setUp() { | ||
redisServer.start() | ||
} | ||
|
||
@AfterAll | ||
fun tearDown() { | ||
RedisConnectionManager.close() | ||
redisServer.stop() | ||
} | ||
|
||
@Test | ||
fun givenMessageListener_whenMessagePublished_thenMessageReceived() { | ||
redisSubscriber.subscribeToChannel(channel) | ||
redisPublisher.publishMessage(channel, message) | ||
messageListener.latch.await(500, TimeUnit.MILLISECONDS) | ||
assertEquals(message.content, messageListener.messagesReceived.get(0)) | ||
} | ||
|
||
} |