Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2205

Jedis PubSub OOM issue with a solution from docs

$
0
0

Following (not much really, google and YT for "jedis pubsub" provides only few results) jedis pubsub docs and tutorials I have created most simplest app which we could imagine - simply subscribe to a channel from Redis Sentinel and print it to STDOUT. Here is the code:

Main.class

package org.example;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPubSub;import redis.clients.jedis.JedisSentinelPool;import redis.clients.jedis.exceptions.JedisException;import java.util.HashSet;import java.util.Set;public class Main {    public static void main(String[] args) {        Set<String> sentinels = new HashSet<>();        sentinels.add(RedisCredentials.HOSTNAME +":"+RedisCredentials.PORT);        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("mymaster", sentinels, RedisCredentials.PASSWORD);        Jedis jedis = null;        try{            jedis = jedisSentinelPool.getResource();            JedisPubSub jedisPubSub = new MyJedisPubSub();            jedis.subscribe(jedisPubSub, "test");        }catch (JedisException e){            System.out.println(e.getMessage());        }finally {            if (jedis != null){                jedisSentinelPool.close();            }        }    }}

MyJedisPubSub.class

package org.example;import redis.clients.jedis.JedisPubSub;public class MyJedisPubSub extends JedisPubSub {    @Override    public void onMessage(String channel, String message) {        System.out.printf("Channel %s received message: %s", channel, message);    }    @Override    public void onSubscribe(String channel, int subscribedChannels) {        System.out.printf("Subscribed to %s, subscribed total %d channels.", channel, subscribedChannels);    }    @Override    public void onUnsubscribe(String channel, int subscribedChannels) {        System.out.printf("Unsubscribed from %s, subscribed total %d channels.", channel, subscribedChannels);    }}

The problem is that when I'm receiving messages the used memory is increasing (on linux I check it through ps aux and I see that RSS in constantly increasing while I'm sending messages) until OOM point and I need to restart my application. Unfortunately java examples and docs for redis are like Call of Duty United Offensive mod scene, almost nothing is there. It doesn't matter if I send 10 msg/s or 5000 msg/s, effect is the same. Please help with clearing the memory.

PS jedis version 4.3.1, java 11, (I believe it is server version, not my redis-cli) redis_version:5.0.5

I have tried removing everything not related to pure java and jedis and above example is exactly it. I have searched through google tutorials but every tutorial provide almost identical code as above. Nothing is mentioned about jedis and memory,performance, best practices etc on the google.


Viewing all articles
Browse latest Browse all 2205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>