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