Proper pattern for subscribe, get a message then close down

127 views
Skip to first unread message

Fredrik Wendt

unread,
Aug 13, 2012, 4:04:48 AM8/13/12
to Jedis
Hi guys, first post.

My usage scenario is to use pubsub in Redis for simple communication
between two applications. I previously used JMS/ActiveMQ with out any
issues and now I'm just trying to see if I can replace that with
something else, just for fun.

The problem I have is related to closing down properly after receiving
a message on a channel. What the subscribing part does is this:
* connect
* subscribe
* get a message
* stop subscription, close connection to redis
* parse message and pass part of the message on to some other part of
the application
* end - the application is short lived and essentially is one of many
parties interested in a pass code

Code:
Full actual code at https://gist.github.com/3338025 but this is an
outline:

// start subscription in one thread
Redis redis = new Redis(...);
final CountDownLatch latch = new CountDownLatch();
final Collection<String> messageContainer = new ArrayList<String>();
new Thread(new Runnable() {
redis.subscribe(new JedisPubSub() {
...
public void onMessage(String channel, String message) {
messageContainer.add(message);
latch.countDown();
this.unsubscribe();
}
}, "channel");
}).start();

// let main program go on do some work that will trigger another
application to publish a message to "channel"
triggerSomeOtherApplication();

// then wait for the message
latch.await();
String message = messageContainer.iterator().next();
redis.quit();
sysout(message);

Exception:
xception in thread "main"
redis.clients.jedis.exceptions.JedisConnectionException: It seems like
server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:
91)
at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:
110)
at redis.clients.jedis.Protocol.process(Protocol.java:63)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:
152)
at redis.clients.jedis.Jedis.quit(Jedis.java:75)

I'm using redis.client/jedis 2.0.0 from some public maven repository.

$ aptitude show redis-server | grep Version
Version: 2:2.0.0~rc2-1

Thanks in advance to any pointers on how to properly subscribe, get
one message, unsubscribe and close down.

/ Fredrik

Jonathan Leibiusky

unread,
Aug 13, 2012, 10:31:11 AM8/13/12
to jedis...@googlegroups.com
The problem seems to be that the "quit" command is called some time after you opened the connection to redis. By default redis will close idle connections after 300 seconds (you should check your configuration).
You have several ways to handle this:
1. make redis to not close any connections ever.
2. increase that time and make sure you won't get over it.
3. have a try & catch block when calling quit command

Fredrik Wendt

unread,
Aug 13, 2012, 4:30:52 PM8/13/12
to jedis...@googlegroups.com
Thanks Jonathan for you quick reply!

I went with a variant of option 3 with try catch: I wrote another class that simply sets up one thread to listen/subscribe, and another to publish, and then takes down the Jedis instances. They both fail and I can live with this, I just want to know if I'm doing anything wrong.

(My program is really not significant in any way, but the problem is that the documentation available doesn't really help you out saying whether you should've called unsubscribe before calling quit (looks like it doesn't matter with the setup I have anyway, might be an explanation ;-)))

The redis server is a vanilla installation under Ubuntu 10.10.


thread / mills since program start / log message
    subscriberThread      5 Connecting
     publisherThread      1 Connecting
     publisherThread     40 Waiting to publish
     publisherThread     41 Ready to publish, waiting one sec
    subscriberThread     40 subscribing
    subscriberThread    624 onSubscribe
     publisherThread   1042 publishing
     publisherThread   1048 published, closing publishing connection
    subscriberThread   1049 Message received
                main   1050 Got message: This is a message
     publisherThread   1050 >>> OH NOES Pub, It seems like server has closed the connection.
    subscriberThread   1051 onUnsubscribe
    subscriberThread   1052 subscribe returned, closing down
    subscriberThread   1053 >>> OH NOES Sub - It seems like server has closed the connection.


Again, thanks in advance for any pointer.

/ Fredrik

Jonathan Leibiusky

unread,
Aug 15, 2012, 6:40:01 PM8/15/12
to jedis...@googlegroups.com
It is basically the same.
If you call quit before calling unsubscribe, redis will unsubscribe your connection automatically.
So it doesn't really matters as far as I can think of.
Reply all
Reply to author
Forward
0 new messages