diff options
| author | mjkwiatkowski <mati.rewa@gmail.com> | 2026-06-17 18:08:56 +0200 |
|---|---|---|
| committer | mjkwiatkowski <mati.rewa@gmail.com> | 2026-06-17 18:08:56 +0200 |
| commit | 4562f52c9b540944200b33d4ffbd60b3cbc5ee79 (patch) | |
| tree | 82d028faf5a0555cb80ce9602890a3257ef695c2 /opendc-common/src/main/kotlin/org/opendc/common | |
| parent | 78a9d920cc8aca951aff798272b0d5e7a2e356b9 (diff) | |
Diffstat (limited to 'opendc-common/src/main/kotlin/org/opendc/common')
| -rw-r--r-- | opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt | 64 |
1 files changed, 46 insertions, 18 deletions
diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt index b659d40a..bf674a33 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/Redis.kt @@ -4,40 +4,68 @@ import com.fasterxml.jackson.dataformat.toml.TomlMapper import redis.clients.jedis.RedisClient import redis.clients.jedis.StreamEntryID import redis.clients.jedis.params.XReadParams +import java.util.Map import java.util.Properties + /** * This class represents the Redis server instance. * @author Mateusz Kwiatkowski * @see <a href=https://redis.io/docs/latest/>https://redis.io/docs/latest/</a> * * @see <a href=https://redis.io/docs/latest/develop/data-types/streams/>https://redis.io/docs/latest/develop/data-types/streams/</a> + * + * @see <a href=https://www.javadoc.io/doc/redis.clients/jedis/latest/index.html>https://www.javadoc.io/doc/redis.clients/jedis/latest/index.html</a> */ @Suppress("DEPRECATION") -public class Redis { - private var properties : Properties +public class Redis private constructor() { + private var jedis : RedisClient? = null + private var properties : Properties? = null init { - properties = TomlMapper().readerFor(Properties().javaClass) - .readValue(Kafka::class.java.getResource("/producer.toml")) + properties = TomlMapper().readerFor(Properties().javaClass).readValue(Kafka::class.java.getResource("/subscriber.toml")) + jedis = RedisClient.create("redis://localhost:${properties?.getProperty("port")}") } - public fun run() { - val jedis : RedisClient = RedisClient.create("redis://localhost:6379") + public companion object { + private var instance: Redis? = null - val res5 = jedis.xread( - XReadParams.xReadParams().block(300).count(100), - object : HashMap<String?, StreamEntryID?>() { - init { - put("${properties.getProperty("table")}", StreamEntryID()) + public fun getInstance(): Redis? { + if (instance == null) { + instance = Redis() + } + return instance + } + } + + public fun readOne() { + while(true) { + val messages = jedis?.xread( + XReadParams.xReadParams(), + Map.of("${properties?.getProperty("table")}", StreamEntryID() + )); + + if (messages != null) { + for (stream in messages) { + for (entry in stream.value) { + if(entry.getFields().get("downtime") != null) { + entry.getFields().get("downtime")?.toDouble()?.let { + if (it > 0) { + println("ID: " + entry.getID()) + } + } + } } - }) + } + } + } + // https://redis.io/docs/latest/develop/use-cases/streaming/java-jedis/ + // In Redis you can subscribe to updates to a stream. + // You should base your application off this. + // You can listen for new items with XREAD - // in Redis you can subscribe to updates to a stream. - // you should base your application off this. - // you can listen for new items with XREAD - println(res5) - jedis.close() + // do not close for now + //jedis.close() } -}
\ No newline at end of file +} |
