diff --git a/README.md b/README.md index 688483d..566eae6 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ repositories { } dependencies { - implementation "de.smartsquare:socket-io-redis-emitter:0.12.0" + implementation "de.smartsquare:socket-io-redis-emitter:0.13.0" } ``` @@ -83,13 +83,14 @@ socket.io-servers and three consuming socket.io-clients. ### Usage in Spring Boot and Jedis -Jedis: +We don't want to rely on the Spring Data Redis abstractions in this project. +Unfortunately, this makes it necessary that you configure the `JedisPool` manually in your Spring Boot application. +Having JedisPool configured, the emitter can be created as follows: + ```kotlin @Bean -fun emitter(redisConnectionFactory: RedisConnectionFactory): Emitter { - val jedisConnectionFactory = redisConnectionFactory as JedisConnectionFactory - val jedisConnection = jedisConnectionFactory.connection as JedisConnection - return Emitter(JedisPublisher(jedisConnection.jedis)) +fun emitter(jedisPool: JedisPool): Emitter { + return Emitter(JedisPublisher(jedisPool)) } ``` diff --git a/src/main/kotlin/de/smartsquare/socketio/emitter/JedisPublisher.kt b/src/main/kotlin/de/smartsquare/socketio/emitter/JedisPublisher.kt index 4af2b59..eb28ca7 100644 --- a/src/main/kotlin/de/smartsquare/socketio/emitter/JedisPublisher.kt +++ b/src/main/kotlin/de/smartsquare/socketio/emitter/JedisPublisher.kt @@ -1,9 +1,9 @@ package de.smartsquare.socketio.emitter -import redis.clients.jedis.Jedis +import redis.clients.jedis.JedisPool -class JedisPublisher(private val jedis: Jedis) : RedisPublisher { +class JedisPublisher(private val jedisPool: JedisPool) : RedisPublisher { override fun publish(channel: String, message: ByteArray) { - jedis.use { it.publish(channel.toByteArray(), message) } + jedisPool.resource.use { it.publish(channel.toByteArray(), message) } } } diff --git a/src/test/kotlin/de/smartsquare/socketio/emitter/EmitterTest.kt b/src/test/kotlin/de/smartsquare/socketio/emitter/EmitterTest.kt index 0503850..2d06d8b 100644 --- a/src/test/kotlin/de/smartsquare/socketio/emitter/EmitterTest.kt +++ b/src/test/kotlin/de/smartsquare/socketio/emitter/EmitterTest.kt @@ -8,6 +8,7 @@ import org.amshove.kluent.shouldBeEqualTo import org.junit.jupiter.api.Test import org.msgpack.core.MessagePack import redis.clients.jedis.Jedis +import redis.clients.jedis.JedisPool import java.time.LocalDateTime import java.time.OffsetDateTime import java.time.ZoneId @@ -20,11 +21,15 @@ class EmitterTest { private val topicSlot = slot() private val pubSlot = slot() + private val jedisPool = mockk(relaxed = true) { + every { resource } answers { jedis } + } + private val jedis = mockk(relaxed = true) { every { publish(capture(topicSlot), capture(pubSlot)) } answers { 1 } } - private val jedisPublisher = JedisPublisher(jedis) + private val jedisPublisher = JedisPublisher(jedisPool) @Test fun `publish string message`() {