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

Jedis pool stops working when another instance uses PubSubs

$
0
0

I'm currently creating 2 java programs which should (but must not) work side by side.For simplicity call them S1 and S2.

S1 is a simple API that pulls or pushes data from/to redis. For this I initialize a JedisPool on startup and later use this pool to interact with redis.

S2 is a system that communicates in realtime with a master using PubSub.

Creating the pool:

private JedisPool jedisPool;public void startup(){        JedisPoolConfig poolConfig = new JedisPoolConfig();        poolConfig.setMaxTotal(128);        jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 10000);}

And using it later

    public void set(String key, String value) {    Jedis jedis = null;    try {        jedis = Main.getInstance().getJedisPool().getResource();        jedis.clientSetname(key);        jedis.set(key, value);    } catch (Exception ignored) {        ignored.printStackTrace();    } finally {        jedis.close();    }}

This works perfectly fine.

Creating the pubSub for system 2:

public static void runPubSub() {    new Thread(() -> {        Jedis jedis = null;        try {            jedis = new Jedis();            JedisPubSub jedisPubSub = new JedisPubSub() {                @Override                public void onMessage(String channel, String message) {                    if (channel.equals(Main.getInstance().getTypeManager().getName())) {                        //handle Command                    }                }                @Override                public void onSubscribe(String channel, int subscribedChannels) {                    System.out.println("Client is Subscribed to channel : " + channel);                    System.out.println("Client is Subscribed to " + subscribedChannels +" channels");                }                @Override                public void onUnsubscribe(String channel, int subscribedChannels) {                    System.out.println("Client is Unsubscribed from channel : " + channel);                    System.out.println("Client is Subscribed to " + subscribedChannels +" channels");                }            };            jedis.subscribe(jedisPubSub, Main.getInstance().getTypeManager().getName());        } catch (Exception ex) {            System.out.println("Exception : " + ex.getMessage());        } finally {            if (jedis != null)                jedis.close();        }    }).start();}

Now to the specific problem:As soon as I start both, S1 and S2, then S1 stops working. S2 works fine.When using only S1 everything works fine.

Can anyone help me out here?


Viewing all articles
Browse latest Browse all 2204

Trending Articles



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