Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis PubSub #1151

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions kotlin-libraries-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
<version>${moshi.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>${lettuce-core.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -161,6 +171,9 @@
<retrofit.version>2.9.0</retrofit.version>
<wiremock-jre8.version>2.35.1</wiremock-jre8.version>
<moshi.version>1.15.1</moshi.version>
<kotlinx-coroutines.version>1.7.1</kotlinx-coroutines.version>
<embedded-redis.version>1.4.3</embedded-redis.version>
<lettuce-core.version>6.5.0.RELEASE</lettuce-core.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.baeldung.redispubsub

data class Message(val content: String)
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()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.redispubsub

import io.lettuce.core.RedisClient
import io.lettuce.core.api.StatefulRedisConnection

object RedisConnectionManager: AutoCloseable {
val redisClient: RedisClient = RedisClient.create("redis://localhost:6379")
val connection: StatefulRedisConnection<String, String> = redisClient.connect()

override fun close() {
connection.close()
redisClient.shutdown()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.redispubsub

class RedisPublisher {

fun publishMessage(channel: String, message: Message) {
val connection = RedisConnectionManager.connection
val syncCommands = connection.sync()
syncCommands.publish(channel, message.content)
println("Message published: $message")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.redispubsub

import io.lettuce.core.pubsub.StatefulRedisPubSubConnection
import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands

class RedisSubscriber(private val messageListener: MessageListener) {

fun subscribeToChannel(channel: String) {
val pubSubConnection: StatefulRedisPubSubConnection<String, String> = RedisConnectionManager.redisClient.connectPubSub()
pubSubConnection.addListener(messageListener)
val asyncCommands: RedisPubSubAsyncCommands<String, String> = pubSubConnection.async()

asyncCommands.subscribe(channel)
}
}
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))
}

}