Redis Pub/sub using Jedis

1,096 views
Skip to first unread message

Neelesh Korade

unread,
Oct 9, 2013, 9:22:28 AM10/9/13
to redi...@googlegroups.com
Hi All

I am writing a listener for Sentinel notifications. I am using Jedis as a client. Most of the examples I could find suggested writing the subscriber in the following form-

new Thread(new Runnable() {
        @Override
        public void run() {
            Jedis subscriberJedis = new Jedis("localhost");
            try {
                subscriberJedis.subscribe(new JedisPubSub() …..,"CC");
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    }).start();

So basically the subscription is done in a separate thread as it is a blocking operation.

A question I get about this is-

Eventually, whenever the JVM running this subscriber thread is shut, would the socket used by the connection get closed automatically (given that it is a perpetually running thread and we are not calling jedis.disconnect() or jedis.quit() before the JVM is shut)?

Thank you
Neelesh



Neelesh Korade

unread,
Oct 9, 2013, 9:33:05 AM10/9/13
to redi...@googlegroups.com
Also, one thing I noticed is that while redis-cli expects the subscription command to specify instance details for some of the channels like +odown, Jedis does not accept it in "channel" parameter firs subscribe method.

What I mean is, redis-cli expects the subscription command to be like this-


+odown mymaster localhost 6379

while Jedis receives the notifications only if we subscribe as shown below-

cacheClient.subscribe(sentinelListener, "+odown");

it doesn't work if I specify something like this-

cacheClient.subscribe(sentinelListener, "+odown mymaster localhost 6379");

Could someone confirm/clarify this observation?

Thank you
Neelesh


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Leibiusky

unread,
Oct 9, 2013, 9:43:56 AM10/9/13
to redi...@googlegroups.com

If I understand correctly it is the same case with a single threaded jedis app and the jvm shuts down. It won't execute a quit or disconnect and redis will close the connection after the configured idle timeout.
Or maybe when the jvm shuts down it send a tcp rst and redis knows that it needs to close the connection.

Either way, redis will take care of it.

Jonathan

Neelesh Korade

unread,
Oct 9, 2013, 9:55:32 AM10/9/13
to redi...@googlegroups.com
Thanks Jonathan, that certainly helps my understanding.
Reply all
Reply to author
Forward
0 new messages